Limited Offer
60% OFF on all plans!

Comments in PHP

Learn how to write single-line, multi-line, and documentation comments in PHP to make your code readable and maintainable.

Single-line Comments

PHP supports two styles of single-line comments: `//` (C-style) and `#` (shell-style). Both do the same thing — everything after the marker on that line is ignored by PHP. Use them for short explanations, reminders, or clarifying what a specific line does.

1. Basic Single-line Comment

Everything after `//` on that line is ignored by PHP.

2. Hash (#) Style Comment

PHP also supports `#` as a single-line comment marker, borrowed from shell scripting.

3. Inline Comment (End of Line)

Comments can appear at the end of a code line to explain a specific value or decision.

4. Debug Notes / TODOs

Common practice: use keywords TODO, FIXME, and NOTE so they are searchable in editors.

Multi-line Comments

Multi-line comments start with `/*` and end with `*/`. They can span as many lines as needed and are perfect for explaining complex logic, adding file-level descriptions, or commenting out a block of code during debugging.

1. Basic Multi-line Comment

A `/* ... */` block can span multiple lines and describe a section of code.

2. Temporarily Disable a Code Block

Wrap a block in `/* */` to stop it from running without deleting it.

3. Warning / Important Notes

Teams often use multi-line comments for prominent warnings that must not be missed.

Documentation Comments (PHPDoc)

PHPDoc comments use `/** ... */` (note the double asterisk on the opening). This is the standard documentation format for PHP — used by IDEs (PhpStorm, VS Code), static analysis tools (PHPStan, Psalm), and API documentation generators. Writing PHPDoc makes your code self-documenting and enables powerful editor autocompletion.

1. Documenting a Function

Use `@param` to describe parameters and `@return` to describe what the function returns.

2. Documenting a Class

PHPDoc can document classes, properties, and methods to describe the full shape of an object.

3. Useful PHPDoc Tags

PHPDoc has many tags that make code self-documenting and improve static analysis.