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

// This is a single-line comment

# This is also a single-line comment

/* This is a 
multi-line comment */




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
// Displays a greeting message
echo "Welcome Home!";
?>

Example: Comment at the End of a Line

<?php
echo "Welcome Home!"; // Displays a greeting message
?>




Multi-line Comments

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

Example: Multi-line Comment

<?php
/* This is a
   multi-line comment
   that explains the code below */
echo "This is a multi-line comment example!";
?>




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
// echo "This will not be displayed.";
echo "This will be displayed.";
?>

Tutorials dojo strip
Scroll to Top