In PHP, comments are useful for explaining code and making it more readable. PHP supports single-line, multi-line, and documentation comments.
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!";
?>
Hello, AIT!
Hello again!
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!";
?>
Multi-line comment example!
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);
?>
15
Using comments effectively helps make the code easier to understand and maintain.