Like any other programming language, PHP has its own comment syntax. There are several kinds of comments, which we will explain in detail. Comments are one of the trivial elements in the success of a program; that’s why it’s important to use them intelligently.
What is a comment in PHP?
A comment, in a programming language, is a line written in natural language (mother tongue of the developer for example) that will not be executed by the interpreter (or the compiler depending on the language used). Its function is to describe or explain a part of the code that would be difficult to decipher in case of maintenance or collaborative work (several developers working on the same program).
Comments are therefore particularly useful for a solitary developer, but they are even more useful when a complete team is working on the same project. Among other things, they allow you to impose nomenclatures and organization in the writing of code for a collaborative project. Moreover, comments ensure an easier maintenance of the program by its author or a third person.
Another strong point of comments is the generation of technical documentation. Indeed, there are applications such as PHPDocumentor which relies on a particular syntax of comments in order to generate the documentation of an application. This saves a lot of time for a development team.
Commenting a code is also part of the good practices to adopt in programming. However, you should not go to the opposite extreme, where every instruction in the code is commented. The clarity and readability of the program would then be affected.
The 1-line comment
There are two kinds of comments. The single-line commentary and the multi-line commentary. Let’s study together the two methods to comment a text on a single line.
// This is a comment in PHP
echo 'Hello World !';
echo 'Hello hello';
// another comment in PHP
PHP offers two ways to comment out text placed on a line. The most used method is the one of the first example with the double slash (//) contrary to the second one using a pound sign (#).
The multi-line comment
It allows to comment a text written on several lines. It is very frequently used by developers. These comments are defined using the /* and */ symbols. The following example illustrates their use.
I am a comment
You can write what you what inside
echo 'Hello World !';
We have defined what a comment is and how it is used depending on whether it is linear or multiline. It is therefore important to always use them in your programs in order to ensure an easy rereading and maintenance of your code.