PHP Comments

Comments are not read or executed as part of the PHP script. Comments helps in reminding the code later.

You can use comments to describe you PHP scripts. Comments also helps in debugging the PHP script later.

PHP Comments Example

Here is a small PHP script, containing all the above three ways of comments in PHP:

<!DOCTYPE html>
<html>
<body>
  
<?php
   echo "<h1>Below are the comments</h1>";
   // This is a single-line comment
   # This is also a single-line comment
   /*
      This is a multiple-line comment block,
      can span over multiple
      lines as required
   */
   // You can also use comments to leave out parts of a code line
   echo "<h1>Below are the paragraphs</h1>";
   echo "<p>This is paragraph 1</p>";
   echo "<p>This is paragraph 2</p>";
?>
  
</body>
</html>

Since comments are not run/executed, so it will produce the following result:

PHP Comments Example

Types of Comments in PHP

Here are basically two type of comments available in PHP:

  • Single line comment
  • multiline comment

Or you can say that there are totally three ways of comments in PHP script, which are:

  • // comments goes here - This is a single line comment
  • # comments goes here - This is also a single line comment
  • /* comments goes here */ - This is a multiline comment

Single Line Comment in PHP

Single line comment in PHP starts with either // or #. Here is an example shows how to include single line comment in both way.

<html>
<head>
   <title>PHP Single Line Comment - CodesCracker</title>
</head>
<body>
<?php 
   // this is first way of single line comment in PHP
   # and this is second way of single line comment in PHP 
   echo "<p>This is single line comment tutorial in PHP</p>";
   echo "<p>As you will see, comment will not present as output</p>";
?>
</body>
</html>

Here is the sample output of the above PHP single line comment example:

php single line comment

As you can see from the above output, the text present as comments in the PHP program have not presented in output. Now let's talk about multiline comment in PHP.

Multiline Comment in PHP

Multiline line comment can be included within /* .... */

In the place of four dots, that is, ...., you can place your multiline comment in the PHP program. Let's take an example to clear this out that how to write multiline comment in any PHP script.

<html>
<head>
   <title>PHP Multiline Comment - CodesCracker</title>
</head>
<body>
<?php 
   /*
      This is multiline comment tutorial in PHP 
      You can include comment expanding as much line 
      as possible, it totally depends on you.
      You can use multiline comment in place of single
      line comment when you required to write some comment 
      that will be expanding in more than one line.
      Multiline line comment is useful when you want to
      write some detail description of your PHP code.
   */ 
   echo "<p>This is multiline comment in PHP.</p>";
   echo "<p>The above multiline line comment willn't be seen as output</p>";
?>
</body>
</html>

Here is the sample run of the above PHP multiline comment example code:

php multiline comment

PHP Online Test


« Previous Tutorial Next Tutorial »