The following code:
<?php//comment
error_reporting(0);
results (at least in 5.2.11) in:
Parse error: syntax error, unexpected T_STRING
… which I find quite bizarre.
Whether or not <?php//comment should be allowed verbatim, I propose that there can be no doubt as to the bugginess of the contents of the parse error.
That’s because there is no T_STRING in sight (though I do understand that productions may form other productions to imbue precedence, e.g. additive-expressions encapsulating multiplication operations, and the like).
Can anyone with an understanding of the PHP grammar explain how this has come about?
See the following snippet which looks for the
<?phpopening tag. Note the requirement of a trailing space, horizontal tab or new line.(Source: zend_language_scanner.l from PHP 5.3.10)
This means that PHP doesn’t see your code as a normal (long) open tag, but rather as having a short open tag.
(Source: zend_language_scanner.l from PHP 5.3.10
The short tag does not require the trailing whitespace.
So, the different parts of your code that PHP sees are
<?,php,//comment,error_reporting…. Thephpis taken to be a constant (which doesn’t exist, see below). Then there is a comment, which is fine. However the next step is to call theerror_reporting()function. This is not something that is allowed to happen at this point and it is now that PHP bails out, complaining that the identifier (theT_STRING)error_reportingwas not expected.To demonstrate this, lets try some code that does work (meaning: PHP code is executed successfully) with
short_open_tagenabled, but won’t withshort_open_tagdisabled.With
error_reportinghigh enough to show notices, you’ll be able to see what goes wrong as the output is something like:With
short_open_tagturned off, the output will be the code in plain text since PHP does not try to execute it.Finally, a little reminder of the description about
T_STRINGin the List of Parser Tokens which says thatT_STRINGis for (don’t ask me why it’s notT_IDENTIFIER,T_BAREWORDor something less confusing):