I have really simple test cases, with closing tag, extra a on purpose:
<?php echo 'test'; a?>
And, w/o closing tag, no closing tag on purpose:
<?php echo 'test'; a
When display_errors = On:
-
With closing tag => test Notice: Use of undefined constant a – assumed ‘a’ in test.php on line 3
-
W/o closing tag => Parse error: syntax error, unexpected $end in test.php on line 3
When display_errors = Off:
-
With closing tag => test
-
W/o closing tag => HTTP 500
Why I get an HTTP 500 error? And why outputs (except error messages) are dependent display_errors option? I thought it only determines if errors will be printed or not. Is this a bug?
The reason for this is as follows:
When PHP sees the closing
?>tag, it adds an implicit semi-colon. This allows for syntax like this:Why is this significant?
Because it means that when you have the closing tag, your stray
ais actually seen asa;.This difference is enough for PHP to have a go at parsing it. It doesn’t recognise it, so it guesses that it’s an unknown constant, and you get the notice message you see.
Without the closing tag, and without a semi-colon, PHP sees an unterminated line of code. This is a syntax error; PHP can’t parse it at all, so it gives up.
Hope that explains the difference.
(on a side-note, that whole unknown-constant-so-I’ll-assume-a-string thing is one of PHP worst mistakes. It’s there for historical reasons, but I really hope they deprecate it at some point in the future; it leaves code wide open to horrendous bugs caused by a minor typo)