How to display or log PHP errors in development and production
To see PHP errors in development, use E_ALL for error reporting and enable display_errors. On a public site, disable display_errors and display_startup_errors and enable log_errors to retain details in server logs. After applying the settings, check the HTTP status, response body, and log separately.
Separate error reporting, display, and logging
error_reporting selects the types of errors to report. display_errors controls whether reported errors go into the response, log_errors controls logging, and error_log is the destination. Keeping E_ALL lets you record errors without printing them to visitors. Check your framework’s debug settings and logger separately. PHP errors and logging PHP error_reporting()
| Setting | Local exercise | Public service |
|---|---|---|
| error_reporting | E_ALL | E_ALL |
| display_errors | On | Off |
| display_startup_errors | Off unless diagnosing startup | Off |
| log_errors | Compare the two demonstration paths | On |
Check where a warning appears in a local test
On a machine with PHP 8, create a new error-demo directory with public and logs subdirectories. The PHP process needs write access to logs. Save the first PHP example as public/development.php. This local example deliberately raises DEMO_WARNING, displays it and continues running.
<?php
declare(strict_types=1);
error_reporting(E_ALL);
ini_set('display_errors', '1');
ini_set('log_errors', '0');
ini_set('html_errors', '0');
header('Content-Type: text/plain; charset=UTF-8');
trigger_error('DEMO_WARNING', E_USER_WARNING);
echo 'Script continued.', PHP_EOL;
Save the next example as public/production.php. It raises the same warning and writes the details to logs/php-errors.log outside the document root. These files are only for comparing the settings in a lab. From error-demo, run the command below to start PHP’s development server. If port 8080 is already in use, change the port in both the command and the request URLs. PHP development server
<?php
declare(strict_types=1);
error_reporting(E_ALL);
ini_set('display_errors', '0');
ini_set('log_errors', '1');
ini_set('error_log', dirname(__DIR__) . '/logs/php-errors.log');
header('Content-Type: text/plain; charset=UTF-8');
trigger_error('DEMO_WARNING', E_USER_WARNING);
echo 'Script continued.', PHP_EOL;
php -d display_startup_errors=0 -S 127.0.0.1:8080 -t public
From another terminal, request the two URLs below. If curl is a PowerShell alias, use curl.exe. Both requests return HTTP 200. The development response contains DEMO_WARNING and Script continued. The production response body contains only Script continued. Confirm that DEMO_WARNING is in the log. Hiding the warning does not resolve its cause.
curl -i http://127.0.0.1:8080/development.php
curl -i http://127.0.0.1:8080/production.php
Verify the HTTP status for an uncaught exception
Save the next example as public/fatal-demo.php and request the URL below while the server is running. The uncaught exception produces HTTP 500. The response body should contain neither DEMO_FATAL nor stack paths, while the log should contain DEMO_FATAL. A real application should send an appropriate failure status and a generic message, with details in restricted logs. Stop the local server with Ctrl+C when you finish.
<?php
declare(strict_types=1);
error_reporting(E_ALL);
ini_set('display_errors', '0');
ini_set('log_errors', '1');
ini_set('error_log', dirname(__DIR__) . '/logs/php-errors.log');
header('Content-Type: text/plain; charset=UTF-8');
throw new RuntimeException('DEMO_FATAL');
curl -i http://127.0.0.1:8080/fatal-demo.php
Find the PHP configuration that actually applies
A syntax error in the same file or a PHP startup error can prevent execution from reaching ini_set(). Use php -l to check syntax and php --ini to see the CLI configuration. PHP-FPM and the Apache PHP module may load different files for web requests. PHP configuration file
php -l public/development.php
php --ini
Use the following config-check.php only in a restricted testing environment, then remove it. SAPI identifies the interface under which PHP runs. Inspect the primary php.ini, additional INI files, and effective settings. ini_get() can show a disabled setting as an empty string or 0, so interpret it alongside other values. PHP configuration modes
<?php
declare(strict_types=1);
header('Content-Type: text/plain; charset=UTF-8');
echo 'SAPI: ', PHP_SAPI, PHP_EOL;
echo 'INI: ', php_ini_loaded_file() ?: '(none)', PHP_EOL;
echo 'Additional INI: ', php_ini_scanned_files() ?: '(none)', PHP_EOL;
foreach (['display_errors', 'display_startup_errors', 'log_errors', 'error_log'] as $key) {
echo $key, ': ', ini_get($key), PHP_EOL;
}
echo 'error_reporting: ', error_reporting(), PHP_EOL;
The example below shows a production php.ini. Use a writable log destination outside the document root. Save the existing configuration, then follow your installation’s procedures for configuration validation and reload or restart. The CLI reads its configuration on the next run. Retest the response and the log after applying the change.
error_reporting = E_ALL
display_errors = Off
display_startup_errors = Off
log_errors = On
error_log = /var/log/my-app/php-errors.log
Fix missing logs and settings that seem ignored
Check whether another configuration overrides yours. Separate framework debug output, echo or var_dump(), and proxy error pages from PHP error display. When logs are missing, check the destination, write permissions, and free space. The default mode of error_log() can return true without proving a message was written. Record only necessary diagnostic information, keeping passwords, tokens, and unsolicited full request bodies out of logs. PHP error_log()




