Comments in PHP

A comment is a note for the person who reads the file, and the engine skips it as if it were not there. A script in PHP has three comment forms, and the choice between them is about the reader rather than about the machine.

Line comments

A double slash comments out everything after it on the same line. Use it for short notes above or beside the code. The full reference lives in Comments in PHP.

<?php // greet the visitor $greeting = "Hello!"; echo $greeting; // prints Hello!

php greet.php

Hello!

A hash sign starts a line comment too, but the double slash is the common style.

A comment runs to the end of the line or to the closing tag, whichever comes first: a ?> inside a // comment closes the PHP block, and the rest of the line is sent to the visitor as text. A quote inside a comment is part of the comment, so it neither opens nor closes a string.

Block comments

Slash-star pairs comment out any span of text, even across lines. They fit longer explanations and code that is paused for a while:

<?php /* Pause the old greeting while the new one is tested. */ echo "New greeting";

php paused.php

New greeting

A block comment does not nest: the first */ ends it, so a commented-out block that already holds a comment leaves the inner */ outside the comment and the file fails to parse. Comment out the outer lines one by one when that happens.

Docblocks

A docblock is a block comment with two stars that documents functions and classes. Editors and tools read its tags:

<?php /** * Build a greeting for the given name. * * @param string $name The name to greet. * @return string The greeting. */ function greet(string $name): string { return "Hello, " . $name . "!"; } echo greet("Ann");

php docblock.php

Hello, Ann!

The tags start with @, and PHPDocumentor, PHPStan and most editors read them: the type of every parameter, the return type, and one line about each. A docblock describes the contract of the function; the code says what happens inside it.

When to comment

Comment the why, not the what. A clear name needs no comment, but a strange limit or a workaround always deserves one:

<?php // Shop API allows 100 calls per minute, so pause here. sleep(1); echo "done";

php pause.php

done

sleep() pauses the script for the number of seconds it is given, so the comment explains a limit of the other side and not the call itself.

Exercises

  1. Add a line comment to greet.php that says where the greeting is used, and check that the output does not change.
  2. Comment out the echo statement of paused.php with a block comment and run the file. What does the terminal print?
  3. Write a docblock with @param and @return for a function that adds two integers, then read the types in the docblock and in the signature.
  4. Put */ inside a block comment and run php -l. Where does the parser say the file ends?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Which pair of characters starts a block comment in PHP?

    Answer

    /* and */

    A block comment opens with /* and ends at the first */, so it can span several lines.

  2. What does a docblock add to a block comment?

    Answer

    One extra star, which lets editors and static analysers read the tags

    The second star of /** marks the comment as documentation, whose @param and @return tags tools read.

  3. What does this program print? <?php // echo "hidden"; echo "shown";

    Answer

    shown

    The first line is a comment that ends at the line break, so only the second echo runs.

  4. What does this program print? <?php echo "before "; /* echo "hidden"; */ echo "after";

    Answer

    before after

    Only the echo statements outside the block comment run, and echo adds no line break.

  5. What does this program print? <?php $text = "no comment here: // is a string"; echo $text;

    Answer

    no comment here: // is a string

    Inside a quoted string the slash and the star are ordinary characters, so the whole string prints.

  6. What does this program print? <?php # the hash line is a comment echo "hash";

    Answer

    hash

    A hash sign comments out the rest of its line, exactly like a double slash.

  7. A block comment already holds /* and */ inside it. What happens?

    Answer

    The first */ ends the comment, and the rest of the file is code

    Block comments do not nest: after the inner */ the parser reads the remaining text as PHP code.

  8. What does this program print? <?php $x = 2; // doubled below echo $x * 2;

    Answer

    4

    A trailing comment ends at the line break, so the echo runs and prints 4.

  9. Which comment fits the line // Shop API allows 100 calls per minute, so pause here. best?

    Answer

    It explains a limit of the outside system, which the code itself cannot show

    A comment earns its place by explaining a reason that the code cannot express; repeating the call would not.

  10. What does this program print? <?php /** * Adds one. */ function plusOne(int $n): int { return $n + 1; } echo plusOne(4);

    Answer

    5

    A docblock is a comment, so it does not change what the function returns.

The code on this page was run with PHP 8.5.13 (php -v) in Alpine Linux, with the commands shown above.

Article author: Arthur Isaev