C4f34e76 516f 40f8 82d6 2961fd41aa79

Comments in PHP

PHP comments are not executed as a part of the code. Their only purpose are to be read by someone who is looking at the code.

PHP comments are used for:

  • 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
  • Leave out some parts of your code – for testing code

PHP supports two types of comments:

  • Single-line comments
  • Multi-line comments

Example

Syntax for PHP comments:

// This is a single-line comment
# This is also a single-line comment

/* This is a
multi-line
comment */

PHP Single-line Comments

Single line comments start with // or #.

Any text after the // or # – and to the end of line, will be ignored.

The following examples uses a single-line comment as an explanation:

Example

A comment before a code line:

// Output welcome message
echo 'Welcome Home!';

Example

A comment at the end of a code line:

echo 'Welcome Home!'; // Output welcome message

Comments to Ignore Code

We can also use comments to prevent code lines from being executed:

Example

Do not display a welcome message:

// echo 'Welcome Home!';

Scroll to Top