Types of comments that we can use in PHP

PHP is a widely used language to create web pages, and it is due to its ease of use, and how easy it is to put it to the test by running it. Regarding the syntax, there are different ways of commenting on the code that, unlike other languages, in PHP there are several ways, and we can use the one that seems simpler, or being guided by a different factor such as the readability of the code. A characteristic of comments in PHP is that they are not displayed when executed, this means that we can freely use them to document our code, unlike comments in JavaScript, CSS, and HTML, where the comments are visible to the end users, of course except, until these are deleted by some kind of script automatically.

php logo

The first type of comments that we can use in PHP, and the most common: In this type of comments, we can only use a single line. It consists of two diagonals together at the beginning of the line.

// Here we can comment.

The second type of comments, and the second most used: Everything between the opening and closing characters, will be comments. It consists of an opening with a slash and an asterisk, and a closing with an asterisk and a slash, these can span several lines and disable the code that is in the middle of the labels.

/*
  Multiple line comments with closing.
  We can write whatever or disable whatever with this.
  echo "This does not work because it is disabled with the comment";
*/

The third type of comments, which is not the worst form for that: These comments are the same as the first type, only, instead of using two characters, it is only one. It consists of starting with a number symbol.

# This is a line comment.
# And it cannot be displayed in a line break.

Advantages of each type

First type: In the first type of comments, we can write short comments, without having to close the comment.

Second type: This is useful when we need to comment a lot of text, for example, with several lines, or some blocks of code so that they do not work.

Third type: Good for short or single line comments, and for many it might be an advantage to just have to put the # character, instead of the two // characters that are used in the first type. All comment options are valid, and their use may vary depending on the type of project we are working on, or the customs of each programmer. A prominent feature of all three types of comments is that they can start anywhere in the document, and not necessarily at the beginning of a line. To verify its behavior, it is worth using a code editor with syntax coloring.

Tags