PHP ยท Chapter 5 of 44

PHP Comments

Comments let you add notes inside your code that PHP ignores when running the script. They help you and other developers understand what the code is doing, and can be used to temporarily disable a line of code while testing.

PHP supports single-line comments using // or #, and multi-line comments wrapped between /* and */, similar to many other C-style languages.

Syntax
// single-line
# also single-line
/* multi
   line */

Single-line comments

Both // and # mark the rest of the line as a comment. // is the most commonly used style in PHP code.

Multi-line comments

Text between /* and */ can span several lines, making it useful for longer explanations or for commenting out blocks of code during debugging.

Example 1 (php)
<?php
  // This prints a greeting
  echo "Hello!";
?>
Output
Hello!

The comment is ignored by PHP and does not affect the output.

Example 2 (php)
<?php
  /* This block
     explains the code below */
  echo "Done";
?>
Output
Done

A multi-line comment can span several lines before the actual code runs.

Key points

  • // and # both create single-line comments.
  • /* ... */ creates a multi-line comment.
  • Comments are ignored during script execution.
  • Comments help explain code and can disable lines while testing.
๐Ÿ’ก Note: Write comments that explain why the code does something, rather than restating what is obvious from the code itself.

๐Ÿ“ Quick Quiz

1. Which symbols can start a single-line comment in PHP?

2. How do you write a multi-line comment?

3. Are comments executed by PHP?