PHP Comments

So we will now discuss about comments, let's consider a scenario where you have written a huge amount of code with different classes, functions, id's, etc. then how would you remember the work of each and every block of code? or suppose you have sent the code to someone else then how would that person will understand the code written by you? You have to write some hints throughout your code to be able to understand it even after some time later or for the person whom you are sending the code. These hints will be written in the form of comments.

Yes, a comment is that part of your program which the Compiler or the Parser ignores. You can write anything in a comment even a piece of code but that will be completely ignored by the PHP Parser. So you can make your code more readable and understandable to yourself and to others by writing hints or explanation of code in the form of comments.

In this tutorial, we will go through different ways of writing a comment in PHP. PHP supports two types of comment: Single Line Comments and Multi Line Comments. Let's go through these one by one:


PHP Single Line Comments

These are short comments and are used wherever short explanations are required. For Single Line Comments you just have to add double slash(//) or hash(#) sign and now whatever you will write to the right side of these signs ,till you hit enter, it will be considered as a comment by the Parser and will be ignored.

If you hit enter and the cursor goes to next line then that won't be counted as a comment, you have to add another double slash(//) or hash(#) sign in front of the text to make it a comment. Look at the example below to know how to add Single Line Comments.

  • // (C++ style single line comment)
  • # (Unix Shell style single line comment)
Example

<!Doctype HTML> <html> <body> <?php // this is C++ style single line comment # this is an Unix Shell style single line comment echo "This is an example of PHP single line comments"; ?> </body> </html>

Output

This is an example of PHP single line comments

PHP Multi Line Comments

If you want to add multiple lines as a comment without going through the hassle of adding double slash(//) or hash(#) signs in every line then you should use Multi Line Comments. These comments will allow you to create a block, inside which you can write multiple lines of comments as required. To add the Multi Line Comments, you need to begin and end the line with (/* Comment goes here */).

Example

<!Doctype HTML> <html> <body> <?php /* Anything placed within comment will not be displayed on the browser; This block will be completely ignored by the PHP Parser. */ echo "Welcome to PHP Multi line comments"; ?> </body> </html>

Output

Welcome to PHP Multi line comments




Color Picker

List of color names supported by all browser »



Character Code

List of character codes to display special character »



Language Code

List of all language supported by all browser »



Country Code

List of Country code supported by all browser »

Latest Updates

PHP Introduction

How to Run a PHP program in Xampp?

How can I create a phpinfo.php page?

How to Define a Variable in PHP

PHP Echo and Print Statements

Working with PHP Operators

Understanding the PHP Data Types

How to Write Comments in PHP

How to Define a Constant in PHP

PHP if...else...elseif Statements