How to Change PHP Error Reporting Level
Controlling how PHP handles and displays errors is essential for debugging during development and securing applications in production. This article provides a direct, step-by-step guide on how to change the PHP error reporting level dynamically within your PHP scripts, as well as globally using configuration files.
Method 1: Using the error_reporting() Function
The most common way to change the error reporting level directly
within a PHP script is by using the built-in
error_reporting() function. This changes the reporting
level for the duration of the script’s execution.
Place this function at the very top of your PHP file, right after the
opening <?php tag:
<?php
// Report all PHP errors (Errors, Warnings, Notices)
error_reporting(E_ALL);
// Report no errors at all
error_reporting(0);
// Report all errors except notices
error_reporting(E_ALL & ~E_NOTICE);
// Report only fatal run-time errors
error_reporting(E_ERROR | E_PARSE);
?>Method 2: Displaying or Hiding Errors on the Screen
Setting the error reporting level only determines which errors are
captured. To choose whether these errors are actually displayed on the
user’s screen, you must use the ini_set() function
alongside error_reporting().
For Development (Show all errors)
During development, you want to see all errors to fix them quickly:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
?>For Production (Hide all errors)
In a live production environment, displaying errors to users is a security risk. You should hide them from the screen and log them to a file instead:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '0');
ini_set('log_errors', '1');
ini_set('error_log', '/path/to/php-error.log');
?>Method 3: Changing Error Reporting via php.ini
If you want to apply these settings globally across all PHP scripts
on your server, you can modify your server’s php.ini
file.
- Locate your
php.inifile. - Search for the following directives and modify them:
; For Development Environments
error_reporting = E_ALL
display_errors = On
; For Production Environments
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
display_errors = Off
log_errors = On
error_log = /var/log/php_errors.log- Restart your web server (Apache, Nginx, or IIS) for the changes to take effect.
Method 4: Changing Error Reporting via .htaccess (Apache)
If you are using an Apache web server and do not have access to the
main php.ini file, you can configure the error reporting
level using a .htaccess file in your website’s root
directory.
Add the following lines to your .htaccess file:
# Enable error display
php_value error_reporting 32767
php_flag display_errors On
# Disable error display (for production)
# php_value error_reporting 22527
# php_flag display_errors Off(Note: 32767 is the numeric value representation for
E_ALL in modern PHP versions).