PHP Comments
Comments are used to insert notes into the code. They have no effect on the parsing of the script. Adding comments in your scripts describe their purpose.
Comments can play several roles:
- They are human-readable notes in the code, such as an explanation of how something works, why you have done something the way you have, or a reminder about something you still need to do.
- Turning one or more lines of code into a comment is a great way to temporarily disable that code while debugging or trying an alternative way of doing something, without having to delete the code altogether.
- Comments can also contain special content for pre-processing tools such as documentation generators or code-testing utilities.
Multiple Line Comments
A comment that starts with /* and ends with */ can span multiple lines.
/* comment text
more comment text */
Your comments can be as long or as short as you need. When PHP sees code that indicates the start of a comment (/*), it ignores everything until it sees the code that indicates the end of a comment (*/).
One possible format for comments at the start of each script is as follows:
/* name: catalog.php
* description: Script that displays descriptions.
* written by: Designer
* created: 2/1/25
* modified: 3/15/25
*/
You should use comments throughout the script to describe what the script does. Comments are particularly important when the script statements are complicated. Use comments such as the following frequently:
/* Get the information from the database */
/* Check whether the customer is over 18 years old */
/* Add shipping charges to the order total */
Single Line Comments
A single-line comment begins with two forward slashes (//) or the pound sign (#).
# This is comment line 1
// This is comment line 2
All text from the # or // to the end of the line is a comment. You can also use # or // in the middle of a line to signal the beginning of a comment. PHP will ignore everything from the # or // to the end of the line. This is useful for commenting a particular statement, as in the following example:
$average = $orderTotal/$nItems; // Compute average price
PHP comments aren't included in the HTML code that is sent to the user’s browser. The user does not see these comments.
The // syntax is the preferred style of single-line comment in modern PHP programming.