PHP Comments

Last Updated : May 4, 2021

PHP Comments provide information about the code block, which helps developers understand what that code block is used for or a short description of the functionality, also disable specific code block from execution. A commented section in the PHP program won’t be executed, and there are multiple ways to add comments in the PHP language.

Why should you add comments in PHP?

  • It helps to disable the code block from running, both single line and multiple lines of code.
  • It helps to add information to make other developers understand better about the code block, Functions, Classes, etc.,
  • It helps to debug the code effectively if it has any issues.
  • It helps to generate the auto-documentation for the whole project.
  • It helps to achieve the Coding Standards, and it is the best practice of programming.

Single Line Comments

Single line comment added by using //(double slash) and # (hash) character(s) in front of any statement. When we add the above-mentioned character(s) in any code block, it will be commented, and the PHP engine does not execute those lines. Please find below the examples,

// This is single line code comment example
# This one also single line code comment example

$fruits = 'apple'; // assigning 'apple' string to variable

# first line checks if specific variable's value equal to 'apple' 
# then echo 'Yes' if its true and 'No' otherwise
if($fruit == 'apple') {
    echo "Yes, its Apple <br>"; // prints only if its true
} else {
    echo "No, it's not <br>"; # prints only if its false

Multi-Line Comments

Multi-line comments are used to comment the multiple lines of code or provide information in more than one line. It usually starts with /* and ends with */ characters. Whatever the code encapsulated within these characters, those codes or information is considered a multi-line comment section, and the PHP engine will not execute it. Also, multi-line comments cannot be nested, and it will consider next closing(*/) as the end from the start(/*) of the comment. Please find below the examples,

/* This is an typical
example of multiple line
comments section
in PHP */

$a = 1 + 2;
/*$b = 2 + 1;
$c = 3 + 1;*/
$d = 4 + 2;

// multi-line comment cannot be nested
/* This is an multi-line
comment */
which has nested commenting code */

phpDoc Block Comments

phpDoc blocks comments used to provide brief documentation about Functions, Classes, and their Variables. This comments format usually starts with /** and ends with */ characters. There are some libraries available to create wonderful documentation from these phpDoc Block comments. This kind of comment enriches the code block documentation and helps developers understand the code’s purpose better than normal comments. Please find below the example,

 * [add description]
 * @param [type] $a [description]
 * @param [type] $b [description]
 * @return [type] $c [description]
function add($a, $b) {
	$c = $a + $b;
	return $c;

# outputs : 3
echo add(1, 2);

We encourage you to check and prefer these Doc-based comments in your program from the beginning to add more value to your code. Please check the phpDocumentor library to generate PHP project documentation automatically if you added phpDoc Block format comments in the code.

Comment PHP in HTML

If you want to add a comment on your PHP code inside HTML pages, you can follow the below recommendations. Because there are few other ways to add comments on the PHP code block in HTML pages. Please find below the examples,

<!-- it works only on HTML code but not PHP code, PHP still runs.
And you can see the output in view source section -->
<label>First Name</label><?php echo $first_name;?>
<!-- <label>Last Name</label><?php echo $last_name;?> -->
<label>Email</label><?php echo $email;?>

<!-- you can comment specific line using both HTML and PHP commenting format -->
<label>First Name</label><?php echo $first_name;?>
<!-- <label>Last Name</label><?php //echo $last_name;?> -->
<label>Email</label><?php echo $email;?>

<!-- or you can do PHP commenting format to comment -->
<label>First Name</label><?php echo $first_name;?>
<?php //<label>Last Name</label><?php echo $last_name;?>
<label>Email</label><?php echo $email;?>

If you are doing HTML commenting format to comment PHP code, then it won’t work. The PHP code still runs and returns the output as HTML. So you have to comment on PHP code using PHP syntax to disable specific code blocks from execution.