Skip to main content
A single-line comment in PHP is a lexical construct that instructs the PHP parser to ignore all text following the comment token up to the end of the current line or the closing PHP tag, whichever occurs first. PHP supports two distinct tokens for single-line comments:
  1. The double forward slash // (C/C++ style).
  2. The hash symbol # (Perl/shell style).
As of PHP 8.0, there is a critical lexical exception regarding the hash (#) token. If the # symbol is immediately followed by an opening square bracket ([), the PHP parser treats the sequence as the start of an Attribute (#[...]), not a single-line comment.

Syntax

Parser Behavior and Termination

The PHP parser terminates a single-line comment upon encountering one of two conditions: 1. Newline Characters The comment ends when the parser reads a newline sequence (\n, \r, or \r\n). Execution resumes on the subsequent line. 2. Closing PHP Tag (?>) If a closing PHP tag appears on the same line as the single-line comment, the comment terminates immediately at the tag. The parser then transitions out of PHP mode, meaning any subsequent text on that line is treated as standard HTML/output rather than commented code.
Because the ?> token overrides the single-line comment, attempting to comment out a block of code that contains a closing PHP tag using // or # will result in the parser exiting PHP mode prematurely, causing syntax errors or unintended plaintext output.
Tired of Poor PHP Skills? Fix That With Deep Grasping!Learn More