Current Location: Home> Latest Articles> PHP Comment Guide: Three Types of Comments with Examples

PHP Comment Guide: Three Types of Comments with Examples

M66 2025-10-31

Overview of PHP Comment Types

In PHP programming, comments are used to explain code and improve readability. PHP provides three types of comments: single-line comments, multi-line comments, and documentation comments, each with specific uses and scenarios.

Single-Line Comments (//)

Single-line comments start with two slashes // and continue to the end of the line. They are suitable for brief explanations of a single line of code.

Example:

// Calculate the sum
$sum = $a + $b;

Multi-Line Comments (/* */)

Multi-line comments start with /* and end with */. They are used to provide more detailed explanations for a block of code or a function.

Example:

/*
 * Calculate the sum of two numbers
 *
 * @param int $a First number
 * @param int $b Second number
 * @return int Sum
 */
function sum($a, $b) {
    return $a + $b;
}

Documentation Comments (/** */)

Documentation comments are a special type of comment, typically used for generating documentation for functions, classes, and properties. They start with /** and end with */, and contain three main parts: description, tags, and body.

  • Description: A brief explanation of the code element's function.
  • Tags: Metadata starting with @, such as @param and @return, providing additional information.
  • Body: Offers more detailed instructions or notes about the code.

Example:

/**
 * Calculate the sum of two numbers
 *
 * @param int $a First number
 * @param int $b Second number
 * @return int Sum
 */
function sum($a, $b) {
    return $a + $b;
}

Conclusion

Understanding the three types of PHP comments can significantly improve code readability and maintainability. Choosing the appropriate comment type for different scenarios makes your code easier to understand and manage.