← Back to PHP Course | Chapter 1: Introduction & Basics | Lesson 7 of 13

PHP Multiline Comments

A single-line comment is fine for a quick note, but sometimes you need to temporarily disable an entire block of code, or write a longer explanation spanning several lines -- retyping // on every single line would be tedious. PHP's multiline comment syntax, /* ... */, lets you wrap any amount of code or text between one opening and one closing marker.

Basic Multiline Comment Syntax

A multiline comment starts with /* and ends with the next */, and everything between those two markers -- across as many lines as needed -- is ignored entirely by the PHP interpreter, exactly like it was never written.

Note: Reach for /* */ whenever a comment needs to span more than one line; for a single short note, // is quicker to type.

Warning: A stray */ appearing earlier than intended (even inside what looks like plain comment text) will close the comment block prematurely.

Example: Basic Multiline Comment Syntax

php
<?php
/* This entire
   multiline comment
   is ignored by PHP */
echo "Only this line runs.";
?>

Disabling a Block of Code for Debugging

A common practical use of /* */ is temporarily disabling a whole block of code while debugging, without deleting it -- wrap the block you want to skip in /* and */, run your script, and simply remove the markers again once you are done testing.

Note: Wrapping a broken or unfinished block in /* */ is a fast way to isolate which part of a script is causing an error.

Warning: Leaving debug-disabled code commented out permanently instead of deleting it clutters a file over time; remove it once you confirm it is no longer needed.

Example: Disabling a Block of Code for Debugging

php
<?php
echo "Before the disabled block\n";
/*
echo "This block is skipped";
echo "So is this line";
*/
echo "After the disabled block\n";
?>

Writing Documentation Blocks Above Functions

Multiline comments are the standard way to write a short documentation block directly above a function, explaining what it does, what parameters it expects, and what it returns -- readable at a glance by anyone opening the file, without needing a separate documentation page.

Note: Keep function documentation blocks focused: what the function does, what each parameter means, and what it returns -- skip restating the obvious.

Warning: A documentation comment that goes stale after the function's behavior changes is worse than no comment at all, since it actively misleads readers.

Example: Writing Documentation Blocks Above Functions

php
<?php
/*
 * Adds two numbers together.
 * Parameters: $a, $b - the numbers to add
 * Returns: the sum
 */
function addNumbers($a, $b) {
    return $a + $b;
}
echo addNumbers(2, 3);
?>

Why Comments Cannot Be Nested

PHP scans for the very first */ after an opening /*, and closes the comment right there -- it does not track nesting depth. This means a comment like /* outer /* inner */ still active code */ actually closes after "inner */", leaving " still active code */" as real, uncommented PHP code, which usually causes a syntax error.

Note: If you need to comment out a block that already contains /* */ comments inside it, switch those inner comments to // temporarily, or comment them out line by line instead.

Warning: A "nested" multiline comment silently breaks in a way that is easy to miss -- always double-check what actually stayed commented after wrapping a large block.

Example: Why Comments Cannot Be Nested

php
<?php
/* outer /* inner */
still active code
?>

Multiline Comments vs Single-Line Comments

PHP supports three comment styles in total: // and # for single-line comments, and /* */ for multiline ones. All three are functionally equivalent in what they hide from the interpreter -- the choice between them is purely about readability and how many lines you need to cover.

Note: Pick // for short inline notes and /* */ for anything spanning more than about two lines; consistency within a project matters more than which style you choose.

Warning: Mixing all three comment styles inconsistently within the same file can make code harder to scan, even though PHP treats them identically.

Example: Multiline Comments vs Single-Line Comments

php
<?php
// Single-line style
# Also single-line style
/* Multiline
   style */
echo "All three styles are functionally identical to PHP.";
?>
Common Mistakes
  1. Nesting one /* */ comment inside another, which PHP does not support -- the first */ it finds closes the whole comment block, silently un-commenting the rest.
  2. Forgetting the closing */ entirely, which comments out everything from that point to the end of the file, including code you meant to keep active.
  3. Using /* */ for a short single-line note when // is simpler and less error-prone to open and close correctly.
Chapter Summary
  • /* */ wraps a comment across multiple lines, unlike // which only comments out the rest of a single line.
  • PHP multiline comments cannot be nested -- the first closing */ ends the whole block.
  • Multiline comments are commonly used to temporarily disable a block of code during debugging, or to write a longer doc-style explanation above a function.
Browser Support

/* */ multiline comments have been part of PHP since its earliest versions and work identically on every PHP-supporting server.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.