PHP Comments

PHP comments are not considered part of the code, since the commented code is not visible to the browser. Basically, it helps the developer to write notes about their code which will later on help them to understand the code. These PHP comments are visible to anyone who visit the source code.

Uses of PHP Comments:

  • Helps the developer to write notes about their code
  • To make it easy for others to understand the source code.
  • Comments are also very useful while debugging the code because programmer can switch off the other code by commenting it and check the code chunk by chunk.

PHP supports single line as well as multi-line comments. Now we will see syntax of both one by one.

Single line PHP Comments

Single line comment is used to comment only single line of code. PHP single line comment begins with // or #, both // and # have same behavior. Lets see an example to understand it clearly.

<?php
// This is a single-line comment
echo "Example of php single line comment <br>";
echo " The commented lines  are not shown as output.";

#This is also a single-line comment
?>
php comments

Multi-line PHP Comments

In PHP multi-line of code can also be commented. PHP multi-line line comment begins with /*, and ends with */. Lets see an example to understand it clearly.

<?php
/* You can comment consecutive multilines at once
using this way of commenting it will help to comment the code while debugging*/

echo "Example of php multiline comment <br>";
echo " The commented lines  are not shown as output.";

?>

The multi-line commenting can also be used to comment a specific part of code in a line. Just like the below example.

<?php
// You can also use multiline comments to leave out parts of a code line.

echo "The commented number is not considered <br>" ;

echo 5 /* + 5 */ + 5;
?>
multiline