PHP Comments

Comments are not executed as a part of the program.

In this chapter you will learn about “How to write comments in PHP?”.

Comments are generally used for documenting purpose. It is also used to explain the functionality of the code.

Comments in PHP help organize and explain parts of your code that might not be clear to a fellow back-end developer.

PHP comments can be used to temporarily deactivate particular sections of code for debugging purposes.

PHP supports single line and multi line comments.

End users do not see PHP comments.

PHP supports several ways of commenting.

PHP Single Line Comments

There are two ways to use single line comments in PHP.

  • // (C++ style single line comment)
  • # (Unix Shell style single line comment)

When to use “PHP Single Line Comments”?

Single-line PHP comments are useful for short notes before a code block or for explaining a single line of code.

PHP Single Line Comment Example

<?php  
# this is Unix Shell style single line comment  
// this is C++ style single line comment  
echo "Welcome to PHP single line comments";  
?>

PHP Multi Line Comments

If you want to comment out a larger section of code or leave a more descriptive comment then you can use “PHP Multi Line Comments”.

Start your multiline comment by writing /* and end it by writing */.

PHP Multi Line Comment Example

<?php  
/* 
Anything placed 
within comment 
will not be displayed 
on the browser; 
*/  
echo "Welcome to PHP multi line comment";  
?>  

Using comments to leave out parts of the code.

<?php
// You can also use comments to leave out parts of a code line
$x = 5 /* + 15 */ + 5;
echo $x;
?>