PHP Comments

Comments are notes inside your PHP code that the interpreter ignores but that help humans understand what the code does.

A comment is a piece of text that PHP skips over when it runs a script. Comments never affect the output; they exist purely for people who read the code later, including your future self. Good comments explain why something is done rather than merely restating what the code obviously does.

Single-line comments

PHP gives you two ways to write a comment that lasts until the end of the current line. You can start it with two forward slashes (//) or with a hash symbol (#). Everything from that marker to the line break is ignored. Both styles behave identically, so choosing one is mostly a matter of team convention.

Two single-line comment styles

<?php
// This line explains the calculation below
$price = 100;
$tax = $price * 0.18; # hash-style comment for the tax rate
echo $tax;
?>

Multi-line comments

When a note spans several lines, wrap it between /* and */. PHP ignores everything in between, no matter how many lines it covers. This block style is handy for longer explanations or for temporarily disabling a chunk of code while you debug.

A block comment and an inline note

<?php
/*
  This function converts a temperature
  from Celsius to Fahrenheit and returns
  the result as a float.
*/
function toFahrenheit($c) {
    return $c * 9 / 5 + 32; /* the standard formula */
}
echo toFahrenheit(25);
?>
Note: Block comments cannot be nested. If you place one /* ... */ inside another, the first */ ends the whole comment and the leftover text will cause a syntax error.

When to use each style

SyntaxScopeTypical use
//To end of lineShort notes and quick clarifications
#To end of lineSame as //, common in config-style files
/* */One or more linesLonger explanations, disabling code temporarily
  • Write comments to explain intent, not to narrate obvious syntax.
  • Keep comments up to date; a stale comment is worse than none.
  • Use block comments to comment out large sections while testing.
  • Prefer clear variable and function names so fewer comments are needed.
Note: A structured block comment that starts with /** is often called a DocBlock. Tools and editors read these to describe functions, parameters, and return types.

Exercise: PHP Comments

Which symbols can start a single-line comment in PHP?