Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 592
frostymarvelous Avatar asked Jul 17 '11 22:07

frostymarvelous


People also ask

What is PHP parse error?

Parse Error (Syntax) Parse errors are caused by misused or missing symbols in a syntax. The compiler catches the error and terminates the script. Parse errors are caused by: Unclosed brackets or quotes. Missing or extra semicolons or parentheses.

How can solve parse error syntax error unexpected in PHP?

A parse error: syntax error, unexpected appears when the PHP interpreter detects a missing element. Most of the time, it is caused by a missing curly bracket “}”. To solve this, it will require you to scan the entire file to find the source of the error.

What is PHP parser?

PHP Parser is a library that takes a source code written in PHP, passes it through a lexical analyzer, and creates its respective syntax tree. This is very useful for static code analysis, where we want to check our own code not only for syntactic errors but also for satisfying certain quality criteria.


2 Answers

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.

like image 59
Francois Deschenes Avatar answered Sep 30 '22 22:09

Francois Deschenes


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.

like image 33
Jason McCreary Avatar answered Sep 30 '22 22:09

Jason McCreary