PHP Comments
Comments in PHP
PHP comments are not executed as a part of the code. Their only purpose are to be read by someone who is looking at the code.
PHP comments are used for:
- Let others understand your code
- Remind yourself of what you did - Most programmers have experienced coming back to their own work a year or two later and having to re-figure out what they did
- Leave out some parts of your code - for testing code
PHP supports two types of comments:
- Single-line comments
- Multi-line comments
Example
Syntax for PHP comments:
// This is a single-line comment
# This is also a single-line comment
/* This is a
multi-line
comment */
PHP Single-line Comments
Single line comments start with // or #.
Any text after the // or # - and to the end of line, will be ignored.
The following examples uses a single-line comment as an explanation:
Example
A comment before a code line:
// Output welcome message
echo 'Welcome Home!';
Try it Yourself »
Example
A comment at the end of a code line:
echo 'Welcome Home!'; // Output welcome message
Try it Yourself »
Comments to Ignore Code
We can also use comments to prevent code lines from being executed: