White screen of death

White screen of death

One of the most frequent issues affecting new developers in PHP is the white screen of death (WSOD).
It looks a lot like this. -->

It's caused by a problem in the PHP program causing the program to fail without emitting any output, not even an error message.

Fortunately, PHP is very good about recording errors, and they can be found in the server error log. This makes for a frustrating experience as one switches between the WSOD, the code, and the server error log trying to identify and fix the problem. What's needed at this stage of development is an immediate display of the problem, on the screen, when it occurs.

Enabling error reporting

Three lines added to the top of a program will handle most error reporting:

ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

This will display run-time errors right there in the page in front of you. It should go without saying that one should disable this on a production site.

Parsing Errors

Runtime display won't pick up errors found when parsing the code, since the program won't run and these lines won't be executed. I'd argue that syntax errors should be picked up in the development environment, and shouldn't go anywhere near a server. (I use PHPStorm. VSCode is free from Microsoft. Other IDEs are available)

However, If you really need to get these errors into your browser you can do so by modifying this line in your PHP.INI file:

display_errors = on