"White Screen of Death" in PHP: How to Make Errors Speak?
Every PHP developer has encountered this situation: the code seems to work, but the screen is blank. No message, no warning. Just a white page and a feeling of mild insanity. Sound familiar? This is the classic "White Screen of Death" (WSOD). Most often, the problem is that PHP error output is disabled.
On Stack Overflow, the question "How do I get PHP errors to display?" has gathered thousands of views. And this is no coincidence: configuring error display is a fundamental skill every developer must know. But simply enabling display_errors is not enough. You need to understand how it affects the performance and security of your application.
In this article, we will cover:
- How to enable PHP errors in different environments (locally, on a server, in frameworks);
- How to configure
error_reportingfor optimal debugging; - Why displaying errors in production is a bad idea;
- How to log errors without losing performance.
Let's go!
Why Doesn't PHP Show Errors? Three Main Reasons
Before diving into the code, let's understand why errors might be hidden. PHP is a flexible language, and its behavior depends on php.ini settings, the script itself, and the server environment.
1. php.ini Settings
The main culprit is the display_errors directive. By default, on many hosting providers, it is set to Off. This is done for security: showing a stack trace to the user in production is bad practice.
2. The error_reporting() Function
Even if display_errors is enabled, the error level may be too low. For example, error_reporting(0) will hide everything. Or you might accidentally filter out the necessary error types (E_NOTICE, E_WARNING).
3. Errors Before Script Execution
The most insidious are syntax errors in php.ini or in included files. If PHP cannot even start the script, it won't reach your settings. In such cases, checking syntax via the console helps: php -l file.php.
How to Enable PHP Error Output: Step-by-Step Guide
Method 1. Editing php.ini (Permanent Settings)
The most reliable way is to modify php.ini. Find the file (usually located in /etc/php/ or /usr/local/etc/php/) and set:
display_errors = Onerror_reporting = E_ALLAfter making changes, be sure to restart the server:
sudo service apache2 restart# or for nginx + php-fpm:sudo service php8.1-fpm restartPros: the setting applies to all scripts on the server.
Cons: requires server access; not suitable for shared hosting.
Method 2. Via .htaccess (for Apache)
If you don't have access to php.ini but have Apache, use .htaccess:
php_flag display_errors onphp_value error_reporting 32767 # E_ALLImportant: this method works if the AllowOverride directive is enabled on the server.
Method 3. Directly in the Script (for Debugging)
The most flexible option is to enable errors in a specific file. Ideal for development:
<?phpini_set('display_errors', 1);ini_set('display_startup_errors', 1);error_reporting(E_ALL);
// Your codeecho $undefined_variable; // Will trigger a Notice?>Note display_startup_errors — it catches errors that occur before script execution (e.g., when including files).
Method 4. Framework Specifics (Laravel, Symfony, WordPress)
Modern frameworks have their own mechanisms. In Laravel
Blogs
Book Recommendations
Read also