Loading..

PHP errors and status codes 0 0

Last updated on Dec 18, 2019 08:32 in DEVELOPMENT
Posted By reza saghafi

What HTTP status code does PHP send in the event of a problem? The answer is: it depends.

The main errors you will come across are parse, fatal, notice and warning. Parse and fatal stop the script, notice and warning don’t.

If any content is sent to the browser then a 200 OK code is always sent regardless of whether there was an error not. The differences occur once you set display_errors to off.

Notices and warnings will never cause a 500 server error but once display_errors is set to off, fatal and parse errors will cause a 500 server error.

The code below demonstrates this.

Note that an easy way to get HTTP status codes in the terminal is with curl -s -o /dev/null -w "%{http_code}\n" https://www.domain.com/test.php

error_reporting(-1);
ini_set('display_errors', 0); // Display errors must be off else you will get a 200 code if PHP outputs a message

// Only run one of the following lines at a time
// The error codes below assume display_errors = off else they all output 200 because content was sent to the browser

// (200 HTTP status code) Warning: scandir(I do not exist): failed to open dir: No such file or directory
scandir('I do not exist');

// (200 HTTP status code) Notice: Undefined variable: foo
$foo += 1;

// (500 HTTP status code) Fatal error: Call to undefined function foo()
foo();

// (500 HTTP status code) Parse error: syntax error, unexpected '$bar' (T_VARIABLE)
$foo = 1 $bar =

** The time is base on America/New_York timezone