PHP Comments

Comments in PHP are lines that are not executed as part of the program. Their sole purpose is to be read by someone reviewing the code.

Tutorials dojo strip



Uses of Comments

Comments can help:

  • Others Understand Your Code: Providing context and explanations.
  • Self-Reminder: Reminding yourself of what you did when you revisit the code later.
  • Excluding Code: Temporarily leaving out parts of your code.




Types of Comments in PHP

PHP supports various methods for adding comments:

Syntax for Comments

PHP




Single Line Comments

Single line comments begin with //. Any text following // up to the end of the line will be ignored by PHP.

You can also use # for single line comments, though // is more commonly used.

Example: Comment Before the Code

PHP

Example: Comment at the End of a Line

PHP




Multi-line Comments

Multi-line comments begin with /* and end with */. Everything between these markers is ignored by PHP.

Example: Multi-line Comment

PHP




Using Comments to Exclude Code

You can use comments to prevent specific lines of code from being executed. This can be helpful when debugging or testing different parts of your script.

Example: Excluding Code

In this example, the line echo "This will not be displayed."; is commented out, so it won’t be executed.

PHP

Tutorials dojo strip