Master PHP comments with practical examples. Enhance code clarity and collaboration, unlocking the power of effective commenting in your PHP scripts.
A comment in PHP code is a line that is not executed as a part of the program. Its only purpose is to be read by someone who is looking at the code.
Comments can be used to:
PHP supports several ways of commenting:
Syntax for comments in PHP code:
// This is a single-line comment
# This is also a single-line comment
/* This is a
multi-line comment */
Single line comments start with //
.
Any text between //
and the end of the line will be ignored (will not be executed).
You can also use #
for single line comments, but in this tutorial we will use //
.
The following examples uses a single-line comment as an explanation:
A comment before the code:
// Outputs a welcome message:
echo "Welcome Home!";
A comment at the end of a line:
echo "Welcome Home!"; // Outputs a welcome message
We can use comments to prevent code lines from being executed:
Do not display a welcome message:
// echo "Welcome Home!";
#php