How to write comments in PHP | comments in php

How to write comments in PHP | comments in php

Comments in PHP

A comment in PHP code is a line that is not executed as a part of the program. Its only purpose is to be read by someone who is looking at the code.

 

Comments can be used to:

  • Let others understand your code
  • Remind yourself of what you did – Most programmers have experienced coming back to their own work a year or two later and having to re-figure out what they did. Comments can remind you of what you were thinking when you wrote the code

 

PHP supports several ways of commenting:

PHP Single Line Comments

There are two ways to use single line comments in PHP.

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

 

// this is C++ style single line comment 

# this is Unix Shell style single line comment 

echo "Welcome to PHP single line comments"; 

 

 

PHP Multi Line Comments

In PHP, we can comments multiple lines also. To do so, we need to enclose all lines within /* */. Let’s see a simple example of PHP multiple line comment.

/*Anything placed

within comment

will not be displayed

on the browser;*/ 

 

PHP: Using comments to leave out parts of the code:

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

$x = 5 /* + 15 */ + 5;

echo $x;  

 

Output:

10

 

Standard PHP Syntax

A PHP script starts with the <?php and ends with the ?> tag.

The PHP delimiter <?php and ?> in the following example simply tells the PHP engine to treat the enclosed code block as PHP code, rather than simple HTML.

// Some code to be executed

echo "Hello, world!";

 

Othet Example PHP Comments

A comment is simply text that is ignored by the PHP engine. The purpose of comments is to make the code more readable. It may help other developer (or you in the future when you edit the source code) to understand what you were trying to do with the PHP.

PHP support single-line as well as multi-line comments. To write a single-line comment either start the line with either two slashes (//) or a hash symbol (#). For example:

 

// This is a single line comment

# This is also a single line comment

echo "Hello, world!";

You may like

Scroll to Top