Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why does this syntax error return HTTP error 500 when 'display_errors' is on?

Tags:

php

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?

like image 796
jela Avatar asked May 06 '26 22:05

jela


2 Answers

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.

like image 107
DaveRandom Avatar answered May 08 '26 15:05

DaveRandom


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!

like image 40
Joseph Ledesma Gabito Avatar answered May 08 '26 16:05

Joseph Ledesma Gabito



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!