Lesson 5 of 50 – Comments in PHP
10%

Comments in PHP

Comments are notes written inside a program to explain the code. PHP ignores comments during normal execution, so they do not appear in the output. Comments help developers understand, maintain, and debug programs.

Note: PHP supports single-line comments using // and #, and multi-line comments using /* ... */.
What are Comments in PHP?

A comment is text in the source code that explains what the program does. It is intended for developers and is not executed as PHP code.

Example:
<?php
// This is a comment
echo "Hello World!";
?>
Output:
Hello World!

The comment explains the code, while the echo statement displays the message.

Single-Line Comments Using //

The double slash (//) starts a single-line comment. PHP ignores the text after it until the end of the line.

Example:
<?php
// Display a welcome message
echo "Welcome to PHP";

// Display a second message
echo "<br>";
echo "Keep Learning!";
?>
Output:
Welcome to PHP
Keep Learning!

Use // when you want to explain a statement or add a short note.

Single-Line Comments Using #

PHP also supports the hash symbol (#) for single-line comments.

Example:
<?php
# Store the student's name
$name = "Rahul";

# Display the student's name
echo $name;
?>
Output:
Rahul

Both // and # can introduce single-line comments. The // style is commonly used in PHP examples.

Multi-Line Comments

Multi-line comments begin with /* and end with */. They are useful when an explanation needs more than one line.

Example:
<?php
/*
This program displays a message.
It is an example of a multi-line comment.
*/
echo "Learning PHP Comments";
?>
Output:
Learning PHP Comments

Everything between the opening and closing comment markers is treated as a comment.

Comments After PHP Statements

You can write a comment on the same line after a PHP statement. The statement executes, but the comment is ignored.

Example:
<?php
$name = "Amit"; // Store the name
echo $name;     // Display the name
?>
Output:
Amit

This style is useful for short explanations, but avoid writing excessively long comments beside statements.

Using Comments to Temporarily Disable Code

During testing, you may temporarily comment out a statement to prevent it from executing.

Example:
<?php
echo "First Message";

// echo "This message is commented out";

echo "<br>";
echo "Second Message";
?>
Output:
First Message
Second Message

The commented-out statement does not execute. Remember to remove temporary comments when they are no longer needed.

Difference Between Single-Line and Multi-Line Comments
Feature Single-Line Multi-Line
Syntax // or # /* ... */
Purpose Short explanations Longer explanations
Can span multiple lines? No, each line needs a new marker Yes
Executed as PHP code? No No
Example:
<?php
// Single-line comment

# Another single-line comment

/*
Multi-line comment
for a longer explanation.
*/

echo "Comments make code easier to understand.";
?>
Output:
Comments make code easier to understand.
Best Practices for PHP Comments
  • Write comments that explain the purpose of the code.
  • Keep comments short and clear.
  • Update comments when the code changes.
  • Avoid explaining obvious statements unnecessarily.
  • Use meaningful variable names along with comments.
  • Use multi-line comments for longer explanations.
  • Never rely on comments to protect sensitive information.
  • Do not store passwords or secret keys in comments.
Important: Comments are not a security feature. Sensitive information written in source code comments may still be exposed if the source code is disclosed.
Key Points
  • Comments explain PHP code to developers.
  • PHP ignores comments during normal execution.
  • // starts a single-line comment.
  • # also starts a single-line comment.
  • /* ... */ creates a multi-line comment.
  • Comments can be written after statements.
  • Comments can temporarily disable individual statements.
  • Clear comments improve code readability and maintenance.
Practical Assignment

Complete the following tasks:

  1. Write a single-line comment using //.
  2. Write another single-line comment using #.
  3. Create a multi-line comment explaining your program.
  4. Display your name using an echo statement.
  5. Comment out one statement and observe the output.
Practice Example:
<?php
// Store the student's name
$name = "Rahul";

/*
Display the name
on the browser.
*/

echo $name;
?>
Output:
Rahul

Replace Rahul with your own name and run the program on your PHP-enabled server.


🧠 Quick Quiz

1. Which symbol starts a single-line comment in PHP?




2. Which syntax creates a multi-line comment?




3. What happens to a PHP comment during normal execution?