Tutorials › PHP Tutorial › PHP Comments

PHP Comments

7 min read · Lesson 4 of 24

A comment is text in your code that PHP ignores completely. Comments are written for humans: your future self, your classmates, or the developer who takes over your project. Good comments explain why the code does something, not just what it does.

Why comment your code?

  • To explain a tricky calculation, such as how a school fees balance or VAT is worked out.
  • To leave notes for teammates ("this list must stay sorted by date").
  • To temporarily switch off a line while you hunt for a bug.
  • To document functions so editors like VS Code can show helpful tooltips.

Single-line comments

PHP has two styles for one-line comments: // and #. Everything after them, up to the end of the line (or a closing ?>), is ignored.

Example PHP
<?php
// Fees for the term, in Kenya shillings
$fees = 15000;

# A hash also starts a comment
$paid = 9000;

echo "Balance: " . ($fees - $paid); // printed for the parent
Result
Balance: 6000
Open in editor

Comments can sit on their own line or at the end of a line of code, as in the last statement above. The // style is by far the most common in modern PHP.

Note: Since PHP 8, #[ starts an attribute (special metadata), not a comment. To avoid surprises, prefer // for comments.

Multi-line comments

For longer explanations, start with /* and end with */. Everything in between is ignored, even across many lines.

Example PHP
<?php
/*
  Student report card
  -------------------
  Calculates the average of three exam scores
  and prints it rounded to one decimal place.
*/
$math = 78;
$english = 85;
$science = 69;

$average = ($math + $english + $science) / 3;
echo "Average: " . round($average, 1);
Result
Average: 77.3
Open in editor

You can also use /* ... */ in the middle of a line, which is occasionally handy:

$total = $price /* before discount */ * $qty;
Warning: Multi-line comments cannot be nested. In /* outer /* inner */ still outer? */, the comment ends at the first */, and PHP then tries to run still outer? */ as code, causing a syntax error.

Commenting out code

When debugging, you can disable a line without deleting it by turning it into a comment. Here the discount line is "commented out", so it is skipped:

Example PHP
<?php
$price = 2500;
// $price = $price * 0.9;   // 10% discount switched off for now
echo "Price: KSh $price";
Result
Price: KSh 2500
Open in editor

In VS Code, select some lines and press Ctrl + / to comment or uncomment them in one go.

DocBlock comments

A special kind of multi-line comment starts with /** (two stars). These are called DocBlocks. Tools and editors read them to show documentation and catch mistakes. They usually sit right above a function or class.

Example PHP
<?php
/**
 * Converts Kenya shillings to US dollars.
 *
 * @param float $kes  Amount in KES
 * @param float $rate KES per 1 USD
 * @return float      Amount in USD
 */
function toUsd(float $kes, float $rate): float
{
    return round($kes / $rate, 2);
}

echo toUsd(6500, 130);
Result
50
Open in editor

You will learn about functions in the Functions lesson. For now, just notice the shape of a DocBlock: each line starts with *, and tags such as @param and @return describe the inputs and result.

Comments do not reach the browser

PHP comments are removed on the server, so visitors cannot see them in "View page source". HTML comments (<!-- ... -->) are different: they are sent to the browser and anyone can read them.

Example PHP
<!-- An HTML comment: visible in page source -->
<?php // A PHP comment: never sent ?>
<p>Done</p>
Result
<!-- An HTML comment: visible in page source -->
<p>Done</p>
Open in editor
Warning: Never put passwords, API keys or private notes in HTML comments. Even in PHP comments, avoid storing secrets in code; keep them in a configuration file outside your public folder.

Good commenting habits

  • Keep comments short and up to date. A wrong comment is worse than no comment.
  • Explain intent and business rules ("Fees are due before the 5th"), not obvious code ($i++; // add 1 to i).
  • Use DocBlocks for functions and classes that others will call.
Exercise

Turn this line into a single-line comment using two slashes.

 This line is ignored by PHP
Exercise

Complete the multi-line comment.

 This comment
   spans two lines 
Sign in or create a free account to track progress, earn XP, badges and a certificate.