PHP Comments

Comments explain your code and are ignored when the script runs.

Syntax// single line # single line /* multi line */

A comment is text that PHP ignores. Comments help you document what your code does.

Comment styles

  • // text — a single-line comment.
  • # text — an alternative single-line comment.
  • /* text */ — a multi-line block comment.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Explaining why a piece of code exists for the next developer.
  • Temporarily disabling a line while debugging.
  • Documenting functions and parameters with docblocks.

More examples

Single-line comments

Both // and # comment out the rest of the line.

Example · php
<?php
// this is a comment
# so is this
echo "Visible";

Block comment

/* ... */ can span multiple lines.

Example · php
<?php
/*
  Multi-line
  comment
*/
echo "After block";

Docblock over a function

/** ... */ docblocks document functions for IDEs and tools.

Example · php
<?php
/**
 * Add two numbers.
 */
function add($a, $b) { return $a + $b; }
echo add(2, 3);

Discussion

  • Be the first to comment on this lesson.