In PHP, Comments are not executed or skipped, it allows to document the source code or program, so that code becomes more readable and others can easily understand, what is the purpose behind code written in program, and also helps in debugging by commenting a part of code. PHP has two types of comments

  1. Single line comment
  2. Multiline comments

Single line comment

It allows to comment a single line of code or statement in PHP. PHP provides two ways to add single line comment in a program.

  1. // (double forward slash) without space is used to add single line comment
  2. # (hash or pound symbol)
<?php
  // Variable Declaration
  $a = 30;

  # Add two numbers...
  $a = 20 + 50;
  echo $a; // outputs 70
?>

Multiline comments

It allows to comment more than one line of code in PHP. /* */ (forward slash and asterisk without space and then asterisk and forward slash without space) is used to add multiline comments.

<?php
  /*
      Program: Hello world
      Date : 28-10-2024
  */

  $name = "Yourshala";
  echo $name;
  /*
     Program output's only
     name.
  */
?>

Comment code for Debugging

Suppose, you have written a PHP program, you dont know where the error is occurring, in that case, comments are great help.

Use comments to the line or lines of code to stop executing and it helps in finding the error.

<?php

  $a = 20;
  $b = 40;
  $c = 0;

   // Assume, below line has error, So comment it to check,
   // $a = $a * $a;  //After commenting, It won't execute, now you get desired output.
  $b = $a + $b;

  $c = $b;
  echo $c;

?>