PHP Comment Syntax with Single Line and Multiline

PHP Comment Syntax

PHP Comment is used to disable some code part in the PHP block. Suppose you are developing a PHP script and you want to disable some part of the code for debug. In that case you can comment that part of the code temperately. Or sometimes used to provide remark about the code.

This Syntax is very simple and it is like C language. PHP support both Multiline Comment and Single Line Comment.

Sometimes PHP comment is also used to provide remark about the PHP code. At the later stage of development, remark is helpful to better understand the PHP code. Mainly, what this small piece of code is performing.

PHP Single Line Comment

Single line Comment Syntax is start with the // (double slash) symbol, and then whole line is treated as comment.

Example: How we can use Single Line Comment.

<!DOCTYPE html>
<html>
<body>
<p>This code shows, how you can use a comment.</p>
<?php
// echo "Hello World";
echo "String Output from PHP Block.";
// This is a comment. And It will not be executed.
?>
</body>
</html>

In the code above we have used two single line comments. The first comment is used to disable the echo function, and second comment is used to put some information or remark about the code.

PHP Multiline Comment

Multiline Comment Syntax is start with the /* symbol, and end with */ symbol.

Example: How we can use Multi-Line Comment.

<!DOCTYPE html>
<html>
<body>
<p>This code shows, how you can use a multiline comments.</p>
<?php
/* 	This script is developed by Nilesh Chaurasia.
	Date: 03.16.2019 */

echo "String Output from PHP Block.";
/* $x=12;
	echo $x; */
?>
</body>
</html>

In the code above we have used two Multi line comments. The first comment is used to show the script credential and second multiline comment is used to disable the code.

Caution The Multiline Comment are necessary to close or end, otherwise all the code below are treated as comment.

Basic Comment Example
Fig.1 – Comment Example Output Screenshot.

Notice that couple of echo statements are not evaluated by the PHP server, because we commented them.

Note: It is good practice to include one or more than one comments in the file. Believe me, that It will very helpful in recalling exactly what you were thinking when writing and looking at this code a year later. Use single line comments for quick notes about a tricky part in your code and use multiple line comments when you need to describe something in greater detail.

PHP Single Line and Multiline Comment Syntax
Fig.2 – Comment Syntax Example Source Code View in the Browser.

The Fig.2 shows the source code that web browser received after the preprocessing of the PHP file. Note that the PHP block is completely removed only the output of echo statement is included as text. Also you can see that all the comments are completely removed from the source code.

Note: All the browser support the feature to view the source code of a web page. Normally you just have to right click on the web page and select (View Page Source) from the context menu, and then browser shows page source in the new windows or tab.

You can leave a response, or trackback from your own site.

One Response to “PHP Comment Syntax with Single Line and Multiline”

  1. excellent publish, very informative. I’m wondering why the opposite specialists of this sector do not understand this. You should proceed your writing. I am confident, you’ve a huge readers’ base already!|

Leave a Reply to the article


Learn PHP

Learning & Certifications
Follow Us
Facebook Icon   Linked In Icon   Twitter Icon  
Validation and Recognition

Valid CSS! Valid HTML5!          Protected by Copyscape