Comments in PHP helps you organize your code and explain some parts of the code to a fellow developer who might be reading it for the first time, or maybe after some time has passed you want to get back at you code and review it. A comment in PHP is not executed, it's only purpose it to be read by someone or to give an explanation about the written code.

Syntax

In PHP, there are single-line comments and multiline comments.

Single-line Comments

The single-line comments are used to explain a single line of code or as a short note before a code block. Before the comment text, type two forward slashes // and instantly you have a single-line comment. Also you could use a hash symbol # instead of the // to create a single-line comment. The single-line comment could also be added in the same line as the code that you want to be executed, to explain what the line of code is doing. In the example below are displayed different types of writing single-line comments.

// This is a single-line comment
// Another single-line comment

# This is also a single-line comment

echo "This text will be displayed."; //but this comment will not

Multiline Comments

When writing code, there will be time when you want to leave a more descriptive comment. For this purpose, you could use multiline comments, which will allow you to write comments that span across multiple lines. The multiline comment should start with /* and end with */. For example:

/* This is an example of a multiline comment
in PHP that spans
across several lines.
*/

In PHP, as in other coding languages, comments are underused. They may seem like avoidable step that can be skipped, but once you start working as a part of a team on complex and composite projects that are build over a long period of time, using comments will help you figure things out and also save you and the others a lot of time.


php