iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

PHP Comments

PHP supports three comment styles. The parser ignores all of them at runtime.

Three forms

PHP
<?php
// Single-line C-style comment

# Single-line shell-style comment (works the same)

/*
   Multi-line block comment.
   Use for notes that wrap.
*/
echo 'comments are stripped at runtime';

Docblocks

Block comments that start with /** are PHPDoc — read by IDEs, static analysers, and generators:

PHP
/**
 * Apply a discount to a price.
 *
 * @param float $price Pre-discount price in dollars.
 * @param int   $pct   Discount percentage (0–100).
 * @return float       Discounted price.
 */
function discount(float $price, int $pct): float
{
    return $price * (1 - $pct / 100);
}

What to comment

  • Why, not what — the code already shows what.
  • Non-obvious business rules or workarounds.
  • TODO / FIXME markers — IDEs surface them.
  • Public APIs — docblock with @param, @return, @throws.

Comments and templates

Comments inside <?php ... ?> never reach the browser. HTML comments (<!-- ... -->) do — don't leak secrets that way.

Tip: Run a static analyser like PHPStan or Psalm. With PHPDoc + types, they catch bugs before the code runs.

Example

Example
<?php
// Single-line comment
# Also single-line
/* Block
   comment */
echo 'comments stripped at runtime';
Try it Yourself »

Exercise

PHPDoc block comments start with…

/ * @return string */

Test yourself

Q1. Single-line comments start with…
Q2. A PHPDoc block starts with…
Q3. HTML comments (<!-- ... -->) inside templates…

Discussion

Loading…