In this tutorial, you will learn how to write the PHP comments inside your PHP source code. The PHP comment is a block or line created to specify a description to be helpful for another developer who is working on the same project.

Table Of Contents

The PHP comments can take two different shapes, such as multiline comments or line comments, the two both are counting a detail or information about a piece of code.

In the following code, you will see the two both.

<?php 
  /* This is multiline comments */
  // This is a single line comment
  # This is a single line comment
?>

Let’s use each one with an example.

Using a Single Line

The one-line comment can be used at the end of the code line after the semicolon or at the beginning of a PHP block, and that can be like the below code.

<?php 
  $is_that_correct = true;
  $itc = true; // Is That Correct ?
?>

Also, we can use a hash sign to define a single comment.

<?php 
  $is_that_correct = true;
  $itc = true; # Is That Correct ?
?>

So you can use the single-line PHP comment to define what is this line of code doing. In the next section, I am going to cover the multiline comment and some other important information about it.

Using Multiline as a PHP Comments

The multiline comment is a C programming language style. It is placed above the block to specify the function or whatever block information.

<?php 
  /* this function is getting users from database */
  function get_users() {
    $users = array();
    return $users;
  }
?>

With the multiline comment, we can write some information about a specific part of code. And that can be done using the PHPDoc. So let’s understand what that means.

Before getting started with the PHPDoc, I need to show you the common names that are needed to be inside the multiline comment to consider the PHP source code as a quality written standard.

  • @api to specify the public method which can be used as an API source.
  • @author to show who wrote this source code.
  • @category to specify the current group name for the source code block.
  • @copyright appears when you use any source code that has copyright.
  • @license refers to licenses such as MTI and so many others.
  • @link to specify a relation between the current block and another one on the internet by the link.
  • @param refers to the function arguments or class attributes.
  • @version is the release number for the block.
  • @since refers to the age of the code block.
  • @return is the data type that can be returned using the PHP function.

And so many other keys to define the source code description. You can see more from here.

Let’s see how to use that with an example.

<?php 
  /**
  * @author Montasser Mossallem
  * @version 1.0.1
  * @param $id with integer data type
  * @return the user data with array data type.
  */
  function get_users( $id ) {
    $users = array();
    return $users;
  }
?>