PHP Comments

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

A comment in PHP code is the non-executable part of the PHP program. But comments are handy. They can be read by someone who is editing the code, a developer working on her fellow teammate’s code, or even an amateur coder who is just starting by understanding the basics of the individual blocks of code.

Read Also: PHP Syntax

PHP Comments

This article describes how to use 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 are 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 comments are useful to comment on 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.

We hope you have been able to gain a clear perspective on how comments are useful for programming in PHP. Keep reading and implementing further blog posts to strengthen your concepts of PHP programming. Happy Learning!

Leave a Reply

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