PHP Comments
A comment is text in your source code that PHP ignores completely. Comments are written for humans: they explain why code exists, leave notes for teammates, and let you temporarily switch code off while testing.
Three comment styles
| Syntax | Kind | Typical use |
|---|---|---|
// text |
Single line | Short notes at the end of a line or above it |
# text |
Single line | Same as // (shell style, less common) |
/* text */ |
Multi-line | Longer explanations, disabling a block of code |
/** text */ |
DocBlock | Documenting functions, classes and properties |
Single-line comments
Everything after // or # up to the end of the line is ignored:
<?php
// Calculate the price of a pizza order
$pizzas = 2; // number of pizzas
$pricePerPizza = 11; # price in euros
echo "Total: " . $pizzas * $pricePerPizza . " EUR\n";Total: 22 EUR
Note:
#[is special: since PHP 8 it starts an attribute such as#[Override], not a comment. Prefer//for comments to avoid confusion.
Multi-line comments
/* starts a comment that continues until the next */, no matter how many lines it spans. It can even sit in the middle of a line:
<?php
/*
Shipping rules:
- orders of 50 EUR or more ship for free
- smaller orders pay a flat 4.90 EUR
*/
$orderTotal = 42;
$shipping = $orderTotal >= 50 ? 0 : 4.9;
echo "Shipping: " . /* inline comment */ $shipping . " EUR\n";Shipping: 4.9 EUR
Multi-line comments cannot be nested: the first */ always ends the comment, so wrapping code that already contains /* ... */ breaks.
Commenting out code
A quick way to test what happens without a line is to turn it into a comment instead of deleting it:
<?php
$items = ["bread", "milk", "eggs"];
echo "Shopping list:\n";
echo "- " . $items[0] . "\n";
// echo "- " . $items[1] . "\n";
echo "- " . $items[2] . "\n";Shopping list:
- bread
- eggs
Most editors toggle comments on the selected lines with Ctrl+/ (or Cmd+/ on macOS).
DocBlocks
A comment that starts with /** is a DocBlock. Editors and documentation tools read the @ tags inside it to show hints and generate API docs:
<?php
/**
* Converts a temperature from Celsius to Fahrenheit.
*
* @param float $celsius Temperature in degrees Celsius
* @return float Temperature in degrees Fahrenheit
*/
function toFahrenheit(float $celsius): float
{
return $celsius * 9 / 5 + 32;
}
echo toFahrenheit(25) . "\n";77
Writing good comments
- Explain why, not what.
$i++; // add one to iadds nothing;// skip the header rowdoes. - Keep comments up to date. A wrong comment is worse than none.
- Prefer clear names over comments:
$taxRateneeds no explanation,$tdoes. - Remove commented-out code before you share it — version control remembers old versions for you.
Tip: Comments are stripped before the code runs, so they never reach the visitor's browser and never slow down your page. They are different from HTML comments (
<!-- -->), which are sent to the browser.
Test yourself
Which of these is **not** a PHP comment?
// note# note/* note */<!-- note -->
What is a comment beginning with /** usually used for?
- Disabling error messages
- A DocBlock documenting a function or class
- Marking the end of the file
- A nested comment
What does echo 1 + /* 2 + */ 3; print?
- 6
- 4
- 1
- A syntax error