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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With