PHP Comments

Comments are very important to be added while doing programming. It is nothing but a note for the coding which make it easy to understand. It will mainly be helpful at the time of maintenance and in future reference. Especially if you are developing large application, commenting the code is essential to keep track of the application flow.

Based on the commenting style, comments mainly are classified as,

  1. Single line comment
  2. Multi-line comment

php_comments

Single line comment

To add a single line comment, The (//) or hash(#) characters are used. It can be called as inline comment. The code has single-line comment using (//)

<?php
//To write PHP program
echo "Single line comment";
?>

Single line comments will be truncated if there is PHP code block found in that line. The following code snippet shows the scenario.

<?php
//To write PHP program
echo "Single line comment";
//To write a PHP program as <?php echo "Comments" ; ?> which prints Comments
?>

Multi-line comment style

For adding multi-line comment we have to enclose the text within /* and */ characters. This type of comments are used to add a detailed information about a code block. The following PHP script shows how to add multi-line comment.

<?php
/* To write PHP program 
//to print by writing as 
# <?php echo "Multi-line line comment" ; ?> 
which prints Comments */
echo "Multi-line line comment";
?>

While adding multi-line comments the nested /* and */ occurrences will truncate comment lines and cause error.

<?php
/* To write PHP program 
  /*to print by writting as 
  # <?php echo "Multi-line line comment" ; ?> */
which prints Comments */
echo "Multi-line line comment";
?>

This PHP code tutorial was published on April 13, 2013.

↑ Back to Top