How do I get PHP errors to display?
Categories:
Mastering PHP Error Display: A Comprehensive Guide

Learn how to configure PHP to display errors effectively for development and suppress them safely in production environments.
Encountering errors is a natural part of software development. For PHP developers, understanding how to properly display and manage these errors is crucial for efficient debugging and maintaining application stability. This guide will walk you through the various methods to configure PHP error reporting, ensuring you can quickly identify issues during development and provide a clean user experience in production.
Understanding PHP Error Reporting Levels
PHP offers a robust error reporting system that allows you to specify which types of errors should be reported. This is controlled by the error_reporting
directive. Different error levels represent different severities or types of issues, such as warnings, notices, parse errors, and fatal errors. Knowing these levels helps you fine-tune what information PHP provides.
flowchart TD A[PHP Script Execution] --> B{Error Occurs?} B -- Yes --> C{Error Reporting Level Set?} C -- Yes --> D{Error Type Matches Level?} D -- Yes --> E[Display Error / Log Error] D -- No --> F[Ignore Error] C -- No --> G[Default Error Handling] B -- No --> H[Continue Execution]
PHP Error Reporting Decision Flow
Configuring Error Display in Development
During development, you want to see all errors immediately to fix them. This typically involves setting display_errors
to On
and error_reporting
to a comprehensive level like E_ALL
. There are several ways to achieve this, each with its own scope and precedence.
E_ALL
) and display errors (display_errors = On
) in your development environment. This catches potential issues early and prevents silent failures.php.ini
; Recommended for development display_errors = On error_reporting = E_ALL log_errors = Off ; Optional: You might want to log errors even in dev
.htaccess
Recommended for development (if php.ini is not accessible)
php_flag display_errors On php_value error_reporting -1 ; -1 is equivalent to E_ALL
PHP Script
Configuring Error Handling in Production
In a production environment, displaying errors directly to users is a security risk and provides a poor user experience. Instead, errors should be logged to a file, and display_errors
should be set to Off
. This ensures that sensitive information is not exposed and that your application appears robust to end-users, while still allowing you to monitor and address issues.
display_errors = On
in a production environment. This can expose sensitive information about your server configuration, file paths, and database queries to potential attackers.php.ini
; Recommended for production display_errors = Off error_reporting = E_ALL log_errors = On error_log = /var/log/php/error.log ; Ensure this path is writable by the web server user
.htaccess
Recommended for production (if php.ini is not accessible)
php_flag display_errors Off php_value error_reporting -1 php_flag log_errors On php_value error_log /var/log/php/error.log
PHP Script
Precedence of Configuration Settings
It's important to understand the order in which PHP processes configuration settings. Settings defined in php.ini
are global. .htaccess
files can override php.ini
settings for specific directories. Finally, ini_set()
calls within a PHP script have the highest precedence but only apply to that specific script's execution.
1. Check php.ini
location
Create a phpinfo()
file (<?php phpinfo(); ?>
) and access it in your browser. Search for 'Loaded Configuration File' to find the active php.ini
.
2. Modify php.ini
Edit the php.ini
file directly. Locate display_errors
and error_reporting
and set them as desired. Remember to restart your web server (Apache, Nginx, PHP-FPM) for changes to take effect.
3. Use .htaccess
for directory-specific settings
If you don't have access to php.ini
or need different settings for a subdirectory, create or modify a .htaccess
file in the relevant directory. Use php_flag
for boolean values and php_value
for string/numeric values.
4. Apply ini_set()
in scripts
For temporary debugging or very specific script requirements, use ini_set()
at the beginning of your PHP script. This overrides all other settings for that script's execution.