PHP Comment (Detailed Explanation with Examples)

This topic will give you another helpful insight into PHP programming. It will give you a well-detailed explanation regarding the proper application of PHP comments.

Like other languages, PHP is also able to let you create notes and labels in between our codes and programs. This technique is called program comment.

What is a PHP Comment?

PHP comment is a line of phrase with a // symbol or a block of sentences with /* comment */ symbol.

This comment can be placed within the program or outside of it.

Moreover, any interpreters and compilers of the language completely ignore comments when we run the program.

This is because the PHP comments were not considered as part of the program, but as labels to let other developers understand the purpose of a particular line of code.

In simple words, the PHP comment is not a code but can be a label, a discussion, or a note.

It is not a function of PHP programming but an essential way to express every line of code.

Note: Comments are text notes that are added to a program to explain the source code.

Why is the PHP Comment Important?

PHP comments are essential since they are used to document your program as it evolves.

The comments can also help you remember the hard things you just did in your program and help people who come after you understand and keep up with the code.

Also, the most important reason you should start using comments is that:

  • Comments can help assist other programmers in comprehending your code.
  • Comments were useful especially in reminding yourself of your project’s or program’s development.

These explanations resulted from what the majority of programmers have encountered, such as returning to their own work a year or two later and having to figure out what they did.

Learning and labeling what you were thinking when you built the code is greatly facilitated by the comments.

Uses of Comment in PHP

There are a lot of uses for comments and we will highlight some of them which include:

  • Readable PHP code descriptions
  • Directions for other users
  • Program progress reminders
  • Labeling functions and classes
  • Variable descriptions
  • Notes for possible improvements
  • Further program explanation, etc.

You may add your way of using the PHP comment since it is really up to you how would you use them.

The use of comments really depends on the user itself.

Other programmers have their own styles and purposes on why they use and how they use comments in their projects.

Nevertheless, the main concept of commenting on your projects is to help you with how you and others better understand your work and improve it for a later time.

PHP Comment Syntax

This time, we will now discuss how to implement the comments in our projects.

First, you need to know that this PHP comment has two ways of implementation.

These implementations were the single-line comments and the multi-line comments.

See the following syntax of comment when applied to your program.

The syntax for single-line comments:

<?php
echo /* example */ "This is your code.";

// This is a comment

# This will also be your comment
?>

The symbols // and # are both applicable for single-line comments.

However, you might also want to write some comments within the code sometimes, so you may use the /* */ symbol.

The syntax for multiline comments:

<?php
/*This is the correct way on how
would you implement
the comment with
multiple lines. */
?>

To comment on a program with multiple lines, enclose your text or discussion in the /*..*/ symbol.

Single-Line Comments

The syntax of the single-line comment is different from the multi-line comment.

To clarify this difference here’s an example of implementing the single-line comment,

Example:

<?php
// This is a comment
# let's try to add numbers
$a = 10;
$b = 11;
$c = $a + $b;
echo "10 + 11 = " . $c; // this line will display the answer
?>

Multiline Comments

The multiline comment is the comment applicable when you need to give a further discussion on the particular code or progress.

Its syntax is also different from the implementation of single-line comments.

However, you will see the correct implementation of multiline comments through the example code below.

Example:

<?php
/* As a brief notice, this program will help you
how to add two numbers in PHP programming. */
 $a = 10;
$b = 11;
$c = $a + $b;
echo "10 + 11 = " . $c;
?>

How do I comment HTML and PHP together?

Basically, you cannot apply the same syntax of commenting in PHP and HTML.

This is because both of them have their own way of commenting.

However, comments in PHP will be applicable even if its code is enclosed within the HTML tags.

In this way, you may apply the comment within the HTML.

Take a look at the example below to see how can you comment on HTML and PHP together.

<!DOCTYPE html>
<html>
<body>
<?php
/* As a brief notice, this program will help you
how to add two numbers in PHP programming. */
 $a = 10;
$b = 11;
$c = $a + $b;
echo "10 + 11 = " . $c;
?>
</body>
</html>

How do I comment out a block of PHP code?

To comment out a block in a PHP code, you may use the technique shown in the multiline comment.

The multiline comment supports multiple lines, even a block of comments as long as you enclose them with the /*...*/ symbol.

Furthermore, you can also use the sing-line techniques of commenting in PHP.

Just refer to the discussions above to see how can you implement these commenting strategies.

Summary

In summary, we have discussed all the matters that concern PHP commenting techniques.

This discussion has proven that there’s a lot more that we can do in PHP programming aside from writing and running codes.

Furthermore, comments in PHP can help a lot of programmers, not only the advanced ones but also the beginners.

Learning how to use comments in programming could really make a difference especially in improving projects and competing with the rapidly growing technology.

Nevertheless, the topic you have read surely gives you the knowledge you deserve regarding PHP comments. Hope that you’ve learned a lot from this topic, good day!

Leave a Comment