2

given the following script

<?php

ini_set('display_errors','On');
error_reporting(E_ALL);

thisisanerror

?>

I get the expected

Notice: Use of undefined constant error - assumed 'error' in /htdocs/test.php on line 8

but if I add something to the script

<?php

ini_set('display_errors','On');
error_reporting(E_ALL);

error

function test () {

    echo('test');

}

?>

I get

HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request.

Why am I getting a 500 error instead of a normal syntax error in the latter case? Shouldn't display_errors always display the error?

8
  • 2
    Yeah, try posting all the tags too. Could be something that affects the result and is invisible. So far - no other ideas :/ Commented May 19, 2012 at 21:47
  • 1
    Does the first code sample actually say "thisisanerror" or does it just say "error"? The error message suggests the latter. Commented May 19, 2012 at 22:03
  • @octern you're right, I changed it and forgot to update the result Commented May 19, 2012 at 22:15
  • @AndriusNaruševičius looks like DaveRandom solved it below Commented May 19, 2012 at 22:17
  • 1
    @AndriusNaruševičius what I posted is the whole code for both scripts, excluding the <?php and ?> tags, which appear in both scripts. you raise a good point about the tags. I have added them to the scripts for clarification. Commented May 20, 2012 at 5:37

2 Answers 2

7

The second code sample is an outright syntax error. This means PHP will not progress to the point of code execution and will die at parse time. Since no code is executed, the effects of the ini_set() call are not seen, so you do not get the PHP textual error indicating the problem.

A parse error is fatal, and the web server is (rightly) set to handle PHP fatal errors with a 500 response code. Since your PHP script did not produce any output the web server will return it's default error document for the 500 condition.

If you want to see the textual message for parse errors in the browser - and one wouldn't normally do this is production, by the way - you will need to turn error reporting on in php.ini or using a .htaccess file.

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

5 Comments

Is the error in this case just the lack of a semicolon after error?
@octern Yes it would seem so...
Thanks; I was just confused by the fact that adding completely valid code changed an existing error from recoverable to unrecoverable. I guess it's a special case because error was previously at the end of the file.
@octern Interestingly if you omit the closing ?> tag the first code sample E_PARSEs as well, but if you add the closing tag it doesn't. Apparently a closing tag has an implicit semi-colon. To be honest I wasn't sure that just a constant as a statement on it's own was syntactically valid, but apparently it is, although I can't think of a practical use for it.
@octern There are other examples of scenarios where adding valid code can change an error from recoverable to unrecoverable. A simple example is that a script ending with a single line of code lacking a ; will often be a recoverable error, whereas if you add a secod line of code after it, even with a ;, it will be an unrecoverable (syntax) error. I personally find this one of the least intuitive aspects of PHP; if I were running things I'd make the a single line without a ; unrecoverable because it leads to the unintuitive, hard-to-debug scenario you brought up.
2

If forcing php to show error on run time doesn't work for you, you may try other options like setting it in php.ini instead:

error_reporting = E_ALL | E_STRICT
display_errors: On

Good Luck!

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.