Discover the main types of comments in PHP including single-line, multi-line, and PHPDoc with simple examples.
Although PHP comments are visible to humans, the PHP interpreter ignores them when the script executes. They explain, clarify, or remind the code’s goal, author, technique justification, or last change date. In essence, comments are ignored by the PHP parser, which treats them like whitespace. Generally speaking, all save the most basic PHP scripts have comments.
Types of comments in php
Explain the three main comment formats that PHP supports, which are derived from C, C++, and the Unix shell.

C++ Style Single-line Comments: Two forward slashes (//) marks are used to start program. Until it reaches the end of the line or the closing PHP tag?>, the parser treats everything that follows // on the same line as a comment. Short remarks or annotating a single line of code are common uses for this style. // This is a one-line comment in the C++ style.
C Style Multi-line Comments: Hash marks (#) are used to start program. Like //, anything that follows # on the same line is regarded as a comment until the line’s end or the PHP tags that closes it. Unix shell scripting languages also use this technique. Some coding standards severely restrict the use of shell-style comments, which are less frequent than //. # This is a one-line comment in the Shell style.
Multi-line comments in the C style start with /* and finish with */. C-style comments, in contrast to single-line styles, can span many lines and are regarded as comments that fall between these markers. Longer descriptions or commenting out lengthy code blocks are appropriate uses for this style. It is not possible to nest C-style comments. Interestingly, C-style comments extend past PHP tag markers at the end. */ This is a multi-line comment in the C style.
<?php
// This is a C++ style single-line comment .
# This is a shell style single-line comment .
/*
This is a C style
multi-line comment .
It spans across several lines.
Useful for longer notes .
*/
echo "Code execution continues here."; // Comment after a line of code .
/*
// You can comment out code blocks like this .
echo "This line is ignored.";
print "So is this one.";
*/
?>
Best Practices for Commenting:
- Comment as you go
- Functions (what it does, input, output), classes, methods, pieces of code, and difficult or tricky code (explaining why a certain technique was adopted) are all examples of comment files.
- Aim for a reasonable level, where there are enough details to comprehend but not enough to overcrowd the code. Don’t comment on things that are obvious.
- By momentarily turning off specific code segments, comments can be extremely helpful for debugging. Examining comments can also assist in identifying errors in problematic code.
- Consistent indenting and well-written comments make code easier for both you and other people to read and comprehend. In general, it’s better to leave more comments than not, especially when you’re first starting out or sharing code.
Documentation tools such as phpDocumentor use a unique type of multi-line remark called the PHPDoc comment (/**… */) to provide documentation for APIs. For describing items such as arguments, return values, authors, and version information, they contain special @ tags.
<!DOCTYPE html>
<html>
<head>
<title>PHP Comments Example</title>
</head>
<body>
<h1>Demonstrating PHP Comments</h1>
<?php
// This is a C++ style single-line comment
# This is a Shell style single-line comment
/*
* This is a C style
* multi-line comment.
* It can span several lines.
*/
$name = "World"; // Assigning a value to the variable $name
echo "<p>Hello, " . $name . "!</p>"; // Outputting a greeting
/* This comment starts here and includes the closing tag
?>
<p>This line is part of the multi-line comment and will not be displayed.</p>
<?php
// and continues inside the next PHP block [58]. Everything within /* ... */ is ignored.
*/
echo "<p>This line appears after the multi-line comment block.</p>";
// You can use comments to temporarily disable code for testing .
// echo "<p>This line is commented out and will not be executed.</p>";
# This is also a commented-out line.
/* You can comment out blocks:
$x = 1;
$y = 2;
echo $x + $y;
*/
?>
<p>This is plain HTML outside the PHP tags.</p>
</body>
</html>
Only the HTML and the text produced by the echo statements will be included in the output when this code is executed. The PHP interpreter entirely ignores the comment lines and does not include the HTML inside the C-style comment that spans a closing tag in the final output that is given to the browser.