puzzling php parser error
Ok maybe not so puzzling, but here it is.
I was messing around and noticed this, typing just <?php
in a file, just that, no space after that, nothing else just the tag, throws a parse error.
With a single space it works fine. I was wondering if anyone knows why the parser chokes, since it is perfectly oka开发者_如何学Pythony otherwise to omit the closing tag. Thanks.
The PHP documentation says:
In PHP 5.2 and earlier, the parser does not allow the
<?php
opening tag to be the only thing in a file. This is allowed as of PHP 5.3.
With that said, in PHP 5.3, if you have short_open_tags
set to On
in your php.ini
file, the error still shows up.
This answered in the PHP Documentation for Basic Syntax:
In PHP 5.2 and earlier, the parser does not allow the
<?php
opening tag to be the only thing in a file. This is allowed as of PHP 5.3.
However, by the OP it seems that the opening tag + space is allowed (i.e. not the only thing in a file). In addition, from the comments, it would seem that this is not the case for distro versions or otherwised patched.
My PHP version:
$ php -v
PHP 5.3.6 (cli) (built: Mar 17 2011 20:56:13)
Copyright (c) 1997-2011 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies
with Xdebug v2.1.0, Copyright (c) 2002-2010, by Derick Rethans
The code in question:
$ echo -n "<?php" | php
<?php
Adding some more next to <?php
:
$ echo -n "<?php/**/" | php
<?php/**/
or
$ echo -n "<?php;" | php
<?php;
and then a space:
$ echo -n "<?php " | php
(finally empty output).
That PHP version is not giving me a Parse error: syntax error, unexpected $end
type of message for the examples above, but it does with this:
$ echo -n "<?php x" | php -d display_errors=1
Parse error: syntax error, unexpected $end in - on line 1
Hope it helps. In my eyes this looks like that the input is treated just as text until a whitespace follows up the <?php
opening sequence.
精彩评论