Getting Started with PHP Syntax


Using Comments in PHP

In PHP, comments are useful for explaining code and making it more readable. PHP supports single-line, multi-line, and documentation comments.

Single-line Comment

Single-line comments start with two slashes (//) or a hash (#).

<?php
// This is a single-line comment
echo "Hello, AIT!"; // Another single-line comment

# This is also a single-line comment
echo "Hello again!";
?>

Output:

Hello, AIT!

Hello again!

Multi-line Comment

Multi-line comments start with /* and end with */.

<?php
/*
This is a multi-line comment.
It can span multiple lines.
*/
echo "Multi-line comment example!";
?>

Output:

Multi-line comment example!

Documentation Comment

Documentation comments, starting with /**, are often used to describe functions and classes.

<?php
/**
 * This function adds two numbers.
 *
 * @param int $a First number
 * @param int $b Second number
 * @return int Sum of $a and $b
 */
function add($a, $b) {
    return $a + $b;
}

echo add(5, 10);
?>

Output:

15

Using comments effectively helps make the code easier to understand and maintain.