How to Write Comments in PHP

How to Write Comments in PHP. The word comment itself shows its purpose as commenting on something. If we comment on anything in the PHP program file, it will not be run with the program. The compiler will just ignore this comment statement. There are different approaches we can go to commenting on anything in the PHP programming language. PHP has single-line and multiple-line comment options as well. Also, in the single-line comment, we can use the # or / as per our preference. We should always write the relevant language in the comment so that anyone who is interpreting that code/program can understand and appreciate that comment.

Syntax

As we know all programming languages have different types of commenting in their code. Again, we can use the comment in the given ways in PHP.

Read Also: Career in PHP | Developer Salary Survey

Different types of Comments in PHP:

# This is a single-line comment
// This is a single line comment
/*
This is a multiple line comment 
This is a multiple line comment 
… and so on …
*/
; this is a PHP.INI style commenting

How does Comments in PHP work?

The time when we write the comment in the language it will be lighter in view approximately as compared to the actual code. If we have addressed code and the comment in the same file, in this case, the comment will not be executed while operating that PHP file. So let’s dive into the same with an example.

<?php
echo "Hello, This is SoftCodeOn";
// The output of this program is "Hello, This is SoftCodeOn
?>

This will give only ‘Hello, This is SoftCodeOn’ as an output, not the comment part.

Why we should use comments in coding?

We should use the comment in between the PHP code to make sure we had sufficient guidance in comments so that one can clearly read and know about the code. In comment, we can also for the persistence of writing the segment of the code, change the date, modification date, modified by, etc. It is highly prescribed for a developer to use the comment in the programming code so that things can be understood quickly with little or no trouble whenever required.

Example:

In a PHP program code file, we can combine both HTML and PHP. Analyzing this, we will use the HTML (Hypertext Markup Language) commenting method to comment out the HTML part whenever required, and the PHP comments can be used for the PHP relevant code as per the business needs. We can use any kind of PHP comments and the HTML comments in a PHP-HTML file.

<html>
<body>
<h2>How to Write Comments in PHP</h2>
<!-- this is html comment -->
<?php
echo "Hello, This is softcodeon!"; // this is PHP comment
?>
</body>
</html>
Output
How to Write Comments in PHP
Hello, This is softcodeon!

Conclusion

A comment is truly a call of time in the program code. We can use the comment in our PHP program file. This will help us and the other developer if we share our code to make the code more readable. We should use the comment before starting a function or the class as most programming languages have their own code standard. Normally, the comment before any function or the class is called the documentation of that function or the class. So, I hope you will use comments in your program code for better understanding.

3 thoughts on “How to Write Comments in PHP

  1. Thank you for the good writeup. It in truth used to be a
    enjoyment account it. Glance complex to far introduced agreeable from you!
    However, how can we be in contact?

Leave a Reply

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