Signup/Sign In

How to write comments in PHP?

A comment in PHP can not be executed as a part of the program. PHP comments are usually written within the block of the PHP code but they are not executed by the PHP engine. These comments will help the user and the developers to understand the code.

The comments are not displayed on the output window.

The comments are included within the code because it helps other to understand the code.

There are two types of comments in PHP

  • Single-line comment
  • Multi-line comment

Single-line comment

The single-line comment is used to comment on any word or any single-line sentence. The single-line comment is denoted by the double slash (//) and hash (#).

Example

This is an example of single line comment in php.

<!DOCTYPE html>
<html>
<head>
	<title>PHP comments</title>
</head>
<body>
	<?php
	// Single line comment 
	# Another comment
	$str = "Hello World";
	echo($str);
	?>
</body>
</html>

Output


Hello World

Multi-line comment

The multi-line comment is used to comment more than one line within the PHP code. The multi-line comment is denoted by /* and */.

Example

This is an example of multi-line comment in PHP.

<!DOCTYPE html>
<html>
<head>
	<title>PHP comments</title>
</head>
<body>
	<?php
	/* This is a 
	multi-line PHP
	comment*/
	$str = "Hello World";
	echo($str);
	?>
</body>
</html>

Output


Hello World

Conclusion

In this lesson, we have discussed how to add comments within the PHP code. There are two types of comments in PHP. The first one is a single-line comment and the second one is a multi-line comment. Single line comment is used to comment on a single line or a word and multi-line comment is used to comment on multiple lines.



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.