Comments in PHP

Last updated 2 years, 2 months ago | 616 views 75     5

Tags:- PHP

PHP | Comments

A comment in PHP code is a line that is not executed as a part of the program. It's only read by someone who is looking at the code. By reading the comments used in the code, one can easily understand the purpose of the code snippet.

Let's see PHP commenting ways:

single-line comment

To make a single-line comment. Simply start the line with a double forward slashes (//) or hashtag (#). They are generally used for short explanations or notes relevant to the code.

<!DOCTYPE html>
<html>
<body>

<?php
// This is a single-line comment

# This is also a single-line comment


# Add two numbers
$x = 5 + 10;
echo $x; // Output sum value

?>

</body>
</html>

Multiple line comments

Multiple line comments start with a forward slash followed by the asterisk /* and end with the asterisk followed by the forward-slash */. They are generally used to provide more detailed explanations when necessary
 

<!DOCTYPE html>
<html>
<body>

<?php
/* 
Author : Studyzone4U
Purpose: Multiline Comments Demo
Subject: PHP
*/

echo "This is a multiple-lines comment block that spans over multiple lines";

?>

</body>
</html>

You can also use the comments to leave out parts of a code line

<!DOCTYPE html>
<html>
<body>

<?php

// leaving out parts of a code
$x = 5 /* + 10 */ + 10;
echo $x;

?>

</body>
</html>