PHP Multiline Comments
In this page:
/*
multi-line comment
spanning several lines
*/
/**
* Documentation block above a function.
*/
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.
Example: Basic Multiline Comment Syntax
<?php
/* This entire
multiline comment
is ignored by PHP */
echo "Only this line runs.";
?>
Login to try C/C++/Java/PHP code in the editor
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.
Example: Disabling a Block of Code for Debugging
<?php
echo "Before the disabled block\n";
/*
echo "This block is skipped";
echo "So is this line";
*/
echo "After the disabled block\n";
?>
Login to try C/C++/Java/PHP code in the editor
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.
- Keep function documentation blocks focused:
- what the function does
- what each parameter means
- what it returns -- skip restating the obvious.
Example: Writing Documentation Blocks Above Functions
<?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);
?>
Login to try C/C++/Java/PHP code in the editor
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.
Example: Why Comments Cannot Be Nested
<?php
/* outer /* inner */
still active code
?>
Login to try C/C++/Java/PHP code in the editor
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.
Example: Multiline Comments vs Single-Line Comments
<?php
// Single-line style
# Also single-line style
/* Multiline
style */
echo "All three styles are functionally identical to PHP.";
?>
Login to try C/C++/Java/PHP code in the editor
- Nesting one /* */ comment inside another, which PHP does not support -- the first */ it finds closes the whole comment block, silently un-commenting the rest.
- Forgetting the closing */ entirely, which comments out everything from that point to the end of the file, including code you meant to keep active.
- Using /* */ for a short single-line note when // is simpler and less error-prone to open and close correctly.
- /* */ 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.
/* */ multiline comments have been part of PHP since its earliest versions and work identically on every PHP-supporting server.
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: