Comments in PHP with Examples

In PHP, comments are used to insert notes into the code that can be read by humans but are disregarded by the PHP interpreter. For the benefit of other programmers, comments can help clarify the intent and functionality of the code, making it simpler to understand and maintain.

PHP scripts can be described using comments. Comments also aid in later debugging of the PHP script.

Types of Comments in PHP

Following are the two types of comments provided by the PHP language.

Single-line comments in PHP

Single-line comments are one line long. They begin with two forward slashes (//) and go on until the end of the line. For instance:

// It's a single-line comment in PHP.
// This is another single-line comment in PHP.

Multi-line comments in PHP

Multi-line comments begin with /* and end with */. Between these two symbols, everything is regarded as a comment. For instance:

/* It's
a multi-line comment
in PHP.
*/

/*
This is another 
multi-line comment 
in PHP.
*/

PHP comment examples

The following example uses the comments to describe the PHP codes.

PHP Code
<?php 
   /* The following line of PHP code
      prints the text "Hello there 1"
   */
   echo "Hello there 1";

   // The following line of code breaks the line
   echo "<BR>";

   /*
   The following line of code
   does the same job of printing
   the text "Hello there 2"
   on the output
   */
   print "Hello there 2";
?>
Output
Hello there
Hello there

Advantages of comments in PHP

Disadvantages of comments in PHP

PHP Online Test


« Previous Tutorial Next Tutorial »