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.

7
  • My php interpreter doesn't throw a parse error in that situation. Commented Jul 17, 2011 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. Commented Jul 17, 2011 at 22:20
  • No idea why, but I can confirm it with PHP5.3.6+Suhosin, 2.6.32-3-amd64 linux kernel. Commented Jul 17, 2011 at 22:22
  • Definitely puzzling. I wonder why I can't duplicate it. PHP Version 5.3.5-1ubuntu7.2 Commented Jul 17, 2011 at 22:25
  • 3
    @PaulPRO: most common editors add a sneaky newline in there. How does echo -n '<?php' > /tmp/test.php; php /tmp/test.php; work out? Still no error? Commented Jul 17, 2011 at 22:29

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Ack, short_open_tags is indeed the difference between parse error or not in my php version (5.3.6)
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. :)
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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.