5. Commenting in PHP

In PHP a comments is that line which is not execute/read when program run. Main purpose of commenting is to make note. It is viewable for someone when look into the code.

Commenting used for:

  • understanding code structure for other team members.
  • It is also works as a way back machine. when you work on your code after a long time period. It helps you to remind what you think when you was code.

PHP have two type of commenting syntax

Single Line Comments:

If you want of comment a single line in PHP there is two syntax available. you can use // (double forward slash) and # (hash). You can put syntax in between the line but it comment upto the end of line. but remember in echo statement // and # work as a text so it must be out of “” or ‘ ‘.

Example

<?php
    echo "This is single line commenting example.";  // This is a comment
    // echo "this line comment with the help of // (double slash)";
    # echo "This line comment with the help of # (hash).";
?>

Output:

This is single line comments example.
This line comment with the help of # (hash).

Multi Line comments:

PHP also support multiline commenting as HTML. For comment multiline use   /*  (forward slash and then *) as opening tag  and */  (star and then forward slash) as closing syntax. Where you put /* your comment start from there and goes end where it fine closing tag */ .

Example:

<?php
    echo "This is multi line commenting example";
    /* echo "Your comment start from here ";
    echo "This line also a comment because it still not found any comment closing tag";
    echo "In the end of this line we close commenting syntax"; */
    echo "So, this line is not comment";
?>

Output:

This is multi line commenting example
So, this line is not comment

Leave a Reply