PHP Single Line Comment and Multi-line Comments

Comments are essential in any coding world. PHP is also providing commenting option. Comments are helpful to include in our script pages, where we can write the purpose of a code.  In PHP we can define single line comments and multiple line comments.

Single Line Comments

As the name says, these are one line statement.

Suppose I have written a code in the php script, in future I may forget why I have written the codes. Or may be some other programmer might confuse with code and it may take hell lots of time to understand what are the purpose of the codes. So for such situation, we use comments. If we can mention the purpose of the code with one line only then we can use single line comment.

Syntax

Single line comment begins with double forward slash.

Look at the example below. The code below calculates the age of an employee in an organisation. (Ignore the code for now)

Example

<?php

// Following codes determines the age of the employer

$employeeId=GetEmployeeID($userLoggedIn);

$dateOfBirth=GetDateOfBirth($employeeId);

$age=CalculateAge($dateOfBirth);

?>

Note: The first line in the above example is a comment. It is a single line comment. Comments has no impact to present code in the PHP script. It is purely for the programmer. It helps to understand a programmer the purpose of the following code.

Now what if, programmer wants to mention hints that longer than one line??

Multiple Line Comments

As the name says, multiple line comments are helpful to define comments that can not be written in one line.

We can define multiple line comments in two ways.

First Solution

We can define multiple single line comments

<?php

// Following codes determines the age of the employer

// For this first we need to fetch employee id

// Then we need to fetch date of birth

// Then by calling age calculator function we can get the age of the employee.

$employeeId=GetEmployeeID($userLoggedIn);

$dateOfBirth=GetDateOfBirth($employeeId);

$age=CalculateAge($dateOfBirth);

?>

Second Solution

Instead of multiple single line comment we can simply define multiple line comments by using “/*” and “*/”.

Example

<?php

/*

Following codes determines the age of the employer

For this first we need to fetch employee id

Then we need to fetch date of birth

Then by calling age calculator function we can get the age of the employee.

*/

$employeeId=GetEmployeeID($userLoggedIn);

$dateOfBirth=GetDateOfBirth($employeeId);

$age=CalculateAge($dateOfBirth);

?>

Note: The opening multiple comment tag “/*” beginning of the comments and closing multiple comment tag “*/”

In between these tag, the text lines are comments.