PHP Comments

A comment is only a piece of information that does not execute but merely gives the reader of the code some information. Comments make the code easier to understand. PHP allows for both single-line and multiple-line comments.

Usage of Comments in PHP

Writing comments alongside your code is usually advised since they are very helpful. You may learn from the comments what a specific piece of code is meant to do. For every programmer who reads or works on it, comments serve as notes.

PHP Single Line Comments

To add a single-line comment, insert two forward slashes like this (//); anything on the same line following these two slashes will be ignored and not executed.


<!DOCTYPE html>
<html>
<body>
<?php
// This is a single-line comment
?>
</body>
</html>


PHP also supports single-line comments using the number symbol (#).


<!DOCTYPE html>
<html>
<body>
<?php
# This is also a single-line comment
?>
</body>
</html>


PHP Multi Line Comment

Comments that span several lines always start with (/*) and close with (*/). They may go on for as many lines as you need.


<!DOCTYPE html>
<html>
<body>
<?php
/*
This is a multiple-lines comment.
It can go as many lines as you need.
*/
?>
</body>
</html>


A portion of code may also be commented out to prevent execution. You may use this approach to identify the areas of code that contain errors.