8

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 okay otherwise to omit the closing tag. Thanks.

Gordon
  • 312,688
  • 75
  • 539
  • 559
frostymarvelous
  • 2,786
  • 32
  • 43
  • My php interpreter doesn't throw a parse error in that situation. – Paul Jul 17 '11 at 22:19
  • Mine does (`PHP Parse error: syntax error, unexpected $end in /home/chris/test.php on line 1`) PHP version is `5.3.3-7+squeeze3`. – Chris Eberle Jul 17 '11 at 22:20
  • No idea why, but I can confirm it with PHP5.3.6+Suhosin, 2.6.32-3-amd64 linux kernel. – Wrikken Jul 17 '11 at 22:22
  • Definitely puzzling. I wonder why I can't duplicate it. PHP Version 5.3.5-1ubuntu7.2 – Paul Jul 17 '11 at 22:25
  • Hm, seems they _just_ added support for it, might still be broken: [_'In PHP 5.2 and earlier, the parser does not allow the – Wrikken Jul 17 '11 at 22:28
  • 3
    @PaulPRO: most common editors add a sneaky newline in there. How does `echo -n ' /tmp/test.php; php /tmp/test.php;` work out? Still no error? – Wrikken Jul 17 '11 at 22:29
  • Ah, Good point. Thanks Wrikken, I managed to replicate it now. Didn't realize my editor was doing that. – Paul Jul 17 '11 at 22:31

3 Answers3

6

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.

Francois Deschenes
  • 24,816
  • 4
  • 64
  • 61
  • Ack, short_open_tags is indeed the difference between parse error or not in my php version (5.3.6) – Wrikken Jul 17 '11 at 22:32
  • Ah. That explains why I have it though I'm on php 5.3.5 I guess it thinks I'm trying to use shorttags'. And the 'p' after isn't valid php. :) – frostymarvelous Jul 17 '11 at 22:35
2

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.

Jason McCreary
  • 71,546
  • 23
  • 135
  • 174
1

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.

hakre
  • 193,403
  • 52
  • 435
  • 836