2

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?

previous_developer
  • 10,579
  • 6
  • 41
  • 66
  • 3
    Related or duplicate: [PHP emitting 500 on errors - where is this documented?](http://stackoverflow.com/q/3075355) – Pekka Oct 26 '12 at 21:40
  • However I deleted my answer due the downvotes, but anyway I'm sure that the reason for your problem is that `a` which is interpreted as an undefined constant. – rekire Oct 26 '12 at 21:44
  • This isn't a question related to a problem he has. He's just trying to understand why PHP behaves that way. – Matheus Azevedo Oct 26 '12 at 21:48
  • I do not get a 500 error, it has to do with something else he has going. – Anthony Hatzopoulos Oct 26 '12 at 22:00

4 Answers4

0

Without your closing Tag, the php interpreter isn't able to determine where your script ends (thus ending it with a parser error).

But WITH closing that, it will parse 'a' as a Constant. But there's no constant 'a', so it will consider it as a String, and the parser will return this string value. Since there's no variable receiving it, it'll just be thrown away. (Probably the parser will just remove this a, since it doesn't affect the application).

Matheus Azevedo
  • 878
  • 7
  • 18
0

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:

<?php echo "something" ?>

Why is this significant?

Because it means that when you have the closing tag, your stray a is actually seen as a;.

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)

Spudley
  • 166,037
  • 39
  • 233
  • 307
  • Thanks, it makes sense now. I was expecting a blank page after i read your answer, but then i saw [this](http://stackoverflow.com/questions/3075355/php-emitting-500-on-errors-where-is-this-documented#answer-3075398) answer to a related question. – previous_developer Oct 26 '12 at 22:05
0

This is a vulgarity of PHP's parser and grammar.

PHP allows you to omit a semicolon before a T_CLOSE_TAG, so when the PHP parser sees this:

a?>

It assumes you meant this:

a; ?>

It then coerces the undefined constant a to a string and issues an E_NOTICE. (This is a terrible misfeature of PHP.) So this is really equivalent to:

'a'; ?>

Since this is not a fatal error and this expression has no side effects, the program executes as normal.

However, if you omit the close tag, you have a syntax error because there is nothing to signal the end of the expression. Syntax errors are fatal, so 500 Internal Server Error is the response.

The display_errors setting only affects whether the errors are printed to the response (I.e., whether you see the notice and parse error messages.)

Francis Avila
  • 31,233
  • 6
  • 58
  • 96
0

I have a different result. The code as OP says, W/o closing tag, is not causing a 500 error for me.

<?php
error_reporting(0);
ini_set('display_errors', 0);
echo 'test'; a

Instead I get:

Parse error: syntax error, unexpected $end in /my-path/test/index.php on line 4

Anthony Hatzopoulos
  • 10,437
  • 2
  • 40
  • 57