Like any other programming language, PHP has its own comment syntax.
There are many PHP comment types which we will detail. Comments are part of the trivial elements in the success of a program, and that is why it is important to use them wisely.
- What is a comment?
- linear comment
- Multi-line comments
What is a comment?
A comment, in a programming language, is a line written in a natural language (the developer’s native language, 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).
Therefore, feedback is particularly useful for a lone developer, but it is most useful when it comes to an entire team working on the same project. Among other things, they allow you to enforce naming and organization when writing code for a collaborative project. In addition, the comments ensure easier maintenance of the program by its author or a third party.
Another strong point of comments is the generation of technical documentation. In fact, there are apps like PHPDocumentor which relies on a particular comment syntax to generate the documentation for an application. This ensures significant time savings for a development team.
Commenting a code is also part of the good practices to adopt in programming. However, one should not go into the opposite excess where each statement in the code would be commented out. Then clarity and readability of the program would be achieved.
There are two types of comments. The single-line comment and the multi-line comment. Let’s study together the two methods to comment a text in a single line.
<?php // Este es el primer comentario lineal echo 'Hello World !'; # Este es otro comentario lineal echo 'Buenos días mundo, desde srcodigofuente.com!!'; ?>
PHP offers two ways to comment out text placed on a line. The most used method is the first with a double slash (//) instead of the second with the hash sign (#).
Allows commenting on a text written on several lines. It is widely used by developers. These comments are defined by the symbols /* and */. The following example illustrates its use.
<?php /* Este es un comentario escrito en sublime text El siguiente código muestra el mensaje hola mundo. */ echo 'Hello World !'; ?>
We have defined what a comment is and how it is used depending on whether it is linear or multiline. Therefore, it is important to remember to always use them in your programs to ensure easy review and maintenance of your code.