PHP Reference ยท Concepts and Reference
PHP comments
Reference for PHP comments: single-line, multi-line and docblock comments, how attributes differ from # comments, reading docblocks with Reflection.
A comment is text that PHP ignores. You use comments to explain why code does something, to disable code temporarily, and to document functions for people and tools. PHP supports three comment styles, plus one special form that Reflection can read, the docblock.
Good comments explain intent ("why"), not mechanics ("what"). If a line needs a comment to say what it does, a clearer name usually removes the need.
<?php // single line # also single line /* block comment across lines */ /** Docblock: a block comment that starts with two asterisks. */
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Comment styles
| Style | Ends at | Notes |
|---|---|---|
// text |
End of line, or ?> |
The usual single-line style. |
# text |
End of line, or ?> |
Shell style. #[ starts an attribute instead (PHP 8.0+). |
/* text */ |
The first */ |
Cannot be nested. |
/** text */ |
The first */ |
Docblock, read by IDEs, PHPDoc and getDocComment(). |
Docblock tags
Common tags are @param, @return, @throws, @var, @deprecated, @see and @template. Static analyzers (PHPStan, Psalm) read generic types from them, for example @return array<string, int> or @param list<User> $users.
Usage notes
- Block comments do not nest:
/* a /* b */ c */ends at the first*/, and the rest is parsed as code. To disable a block that contains comments, use//on each line or anif (false) { }. #[Attribute]is not a comment since PHP 8.0. In PHP 7 it was, so code that must run on both should avoid starting a#comment with[.- Do not put secrets or personal data in comments: they ship with the source.
- Use real types in signatures first and docblocks only for what types cannot say (generics, array shapes, exceptions).
- Remove commented-out code. Version control remembers it.
- Docblocks are not required by PHP. Frameworks may use them (annotations in older Doctrine and Symfony code), but attributes have replaced that in modern code.