1. php
  2. /basics
  3. /comments

Introduction to PHP Comments

Definition

In PHP, comments are used to add notes or explanations to the code for the benefit of other developers who may read the code. Comments do not affect the functionality of the code and are ignored by the PHP interpreter when the code is executed.

Types of Comments and Examples

There are two types of comments in PHP: single-line comments and multi-line comments.

Single-line comments start with // or # and continue to the end of the line. For example:

// This is a single-line comment
# This is also a single-line comment

Multi-line comments start with /* and end with */. For example:

/*
This is a multi-line comment.
It can span multiple lines.
*/

Multi-line comments can span multiple lines and can be used to comment out large blocks of code or to add detailed explanations about the code.

PHP also supports "Docblock" comments, which are a specific type of multi-line comment that starts with /** and ends with */ . Docblock comments are commonly used to add documentation for functions, classes, and methods in a structured format that can be parsed by tools such as phpDocumentor.

<?php
    // This is a single-line comment
    # This is also a single-line comment
    
    /*
    This is a multi-line comment.
    It can span multiple lines.
    */
    echo "Hello, world!";
    
    /**
     * This is a Docblock comment.
     * It's used to document the code.
     * It can be parsed by tools such as phpDocumentor
     */
    function greet($name)
    {
        echo "Hello, $name!";
    }
    greet("John Doe");
?>

In this example, there are three types of comments: single-line comments, multi-line comments, and docblock comments. The first two lines are single-line comments, the third line is the start of a multi-line comment, the Docblock comment is used before the function greet, the multi-line comment spans multiple lines and ends with the closing */ on the last line of the comment. The last line of the example code greet("John Doe"); is not commented and will be executed by the PHP interpreter.

Best Practices

  1. Use comments to explain the purpose and functionality of the code, rather than simply describing what the code is doing.
  2. Keep comments concise and to the point. Avoid using overly verbose or redundant comments.
  3. Use multi-line comments for detailed explanations, and single-line comments for brief notes or explanations.
  4. Use Docblock comments to document functions, classes, and methods in a structured format that can be parsed by tools such as phpDocumentor.
  5. Use comments to indicate the author of the code and the date it was written or last modified.
  6. Keep comments up to date. If the code changes, make sure to update the comments accordingly.
  7. Use comments to indicate any known bugs or limitations of the code.
  8. Comment out any code that is not currently in use, rather than deleting it entirely.
  9. Use comments to indicate any assumptions or dependencies that the code has.
  10. Use comments to indicate any potential security risks or vulnerabilities in the code.