PHP Comments

By IncludeHelp Last updated : November 25, 2023

A PHP comment is a part of code that is not executed by the PHP compiler. The purpose of PHP comments is to write information about the functions, implemented logic, revision history, developers who wrote/updated the code, etc. so that others can understand the code easily. PHP comments are useful to remind you what you have written for which purpose in the code.

Types of PHP Comments

There are two types of PHP comments:

  1. Single-line comments
  2. Multiple-line comments

1. PHP Single-line comments

In PHP, to write a single-line comment, you can use double forward slashes (//) or hash (#) symbol before starting a line.

Syntax

Below is the syntax to insert a single-line comment in PHP code -

// Here, you can write a single-line comment
# Here, you can write a single-line comment

Example: Single-line comments

The following example is demonstrating the single-line comments in PHP.

<?php
// echo statement prints data on the screen
echo "Hello, world!";
# End of echo statement
?> 

Output:

Hello, world!

2. PHP Multiple-line comments

In PHP, to write multiple-line (or, multi-line) comments, you can use /* and */ symbols. The multiple-line comment starts with the combination of forward slash and asterisk symbols (/*) and ends with the combination of asterisk and forward slash symbols (*/).

Syntax

Below is the syntax to insert a multiple-line comment in PHP code -

/*
This is a multiple-lines comment block
Here, you can write any number of lines
Or codes to be commented
*/

Example: Multiple-line comments

The following example is demonstrating the multiple-line comments in PHP.

<?php
/* echo statement prints data on the screen
In the below echo statement, we are printing
"Hello, world!" on the screen*/

echo "Hello, world!";

/* End of the echo statement
After this you can writing anything
that you want
*/
?>

Output:

Hello, world!

Use multiple-line comment as single-line comment

You can also use the multiple-line comment characters/symbols to write a single-line comment in PHP.

Example

In this example, we are using the multiple-line comments characters to write single-line comments.

<?php
/* echo statement prints data on the screen*/
echo "Hello, world!";
/* End of echo statement*/
?>

Output:

Hello, world!

Comment a part of a statement / Comment within a statement

You can also comment on a part of a statement by using the multiple-line comment characters/symbols within a statement.

Example

In this example, we are using commenting a part of PHP statement.

<?php
echo "Hello, world!\n";

$expr = 10 + /*20 +*/ 30 + 40;

echo $expr;
?>

Output:

Hello, world!
80


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.