PHP Comments

PHP comments can be used to describe any line of code so that other developer can understand the code easily. It can also be used to hide any code.

PHP supports single line and multi line comments. These comments are similar to C/C++ and Perl style (Unix shell style) comments.

PHP Single Line Comments

There are two ways to use single line comments in PHP.

  • // (C++ style single line comment)
  • # (Unix Shell style single line comment)
<?php  
// this is C++ style single line comment  
# this is Unix Shell style single line comment  
echo "Welcome to PHP single line comments";  
?>  

Output:Welcome to PHP single line comments

PHP Multi Line Comments

In PHP, we can comments multiple lines also. To do so, we need to enclose all lines within /* */. Let’s see a simple example of PHP multiple line comment.

<?php  
/* 
Anything placed 
within comment 
will not be displayed 
on the browser; 
*/  
echo "Welcome to PHP multi line comment";  
?> 

Output:Welcome to PHP multi line comment

PHP $ and $$ Variables

The $var (single dollar) is a normal variable with the name var that stores any value like string, integer, float, etc.The $$var (double dollar) is a reference variable that stores the value of the $variable inside it.

To understand the difference better, let’s see some examples.

Example 1

<?php  
$x = "abc";  
$$x = 200;  
echo $x."<br/>";  
echo $$x."<br/>";  
echo $abc;  
?>  

Output:

PHP $ and $$ variables

In the above example, we have assigned a value to the variable x as abc. Value of reference variable $$x is assigned as 200.

Now we have printed the values $x, $$x and $abc.

Example2

<?php  
 $x="U.P";  
$$x="Lucknow";  
echo $x. "<br>";  
echo $$x. "<br>";  
echo "Capital of $x is " . $$x;  
?>  

Output:

PHP $ and $$ variables

In the above example, we have assigned a value to the variable x as U.P. Value of reference variable $$x is assigned as Lucknow.

Now we have printed the values $x, $$x and a string.

Example3

<?php  
$name="Cat";  
${$name}="Dog";  
${${$name}}="Monkey";  
echo $name. "<br>";  
echo ${$name}. "<br>";  
echo $Cat. "<br>";  
echo ${${$name}}. "<br>";  
echo $Dog. "<br>";  
?>  

Output:

PHP $ and $$ variables

In the above example, we have assigned a value to the variable name Cat. Value of reference variable ${$name} is assigned as Dog and ${${$name}} as Monkey.

Now we have printed the values as $name, ${$name}, $Cat, ${${$name}} and $Dog.