PHP Comments

Comments in PHP are similar to the comments used in HTML.

A comment in PHP code is not a part of the PHP program. Its only purpose is to be read by someone who is editing the code.

Read Also: PHP Syntax

PHP Comments

This article describes us how to use the PHP comments in code.

  • PHP comment is an essential part of the PHP code.
  • PHP comment makes the code trouble-free to read and maintain.
  • Comments in PHP are similar to the comments used in HTML.
  • PHP comment syntax always begins with a unique character(// or # or /*).
  • PHP comments used to describe any code so that the developer can quickly understand the system.
  • It can also apply to hide the code for temporary purposes.

PHP Supports Two Types of Comment

  1. Single-Line Comment
  2. Multi-Line Comment

Single-Line Comment

PHP single-line comment is useful to comment on just one line of code.

There two ways to use the single-line comment in PHP.

  • // (C++ Style single-Line Comment)
  • # (Unix Shell Style Single-Line Comment)

Example

<html>
<body>
<?php
//echo “C++ Style single-Line Comment”;
#echo “Unix Shell Style Single-Line Comment”;
echo “Welcome Suman!”;
?>
</body>
</html>

Output

Welcome Suman!

Notice that a couple of echo statements are not giving results because we commented them out with a single line comment.

Multi-Line Comment

Like HTML code, the PHP multiple line comment is useful to comment large blocks of code.

PHP multi-line comment begins with /* and ends with */.

Example

<?php  

/* 

Anything placed 

within comment 

will not be executed

*/  

echo "Welcome Suman";  

?>

Output

Welcome Suman

Read More: PHP Variables

Conclusion

PHP comment is an essential part of the PHP code, and it makes the code easier to read and maintain.

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 higher depth than a simple letter.

I hope now you have a real understanding of PHP comments.

Leave a Reply

Your email address will not be published. Required fields are marked *

Read previous post:
php syntax
PHP Syntax

A PHP file contains HTML tags with some PHP code. It is straightforward to create the PHP file. First, create...

Close