error_reporting() / set_error_handler() Since: PHP 4(2000)
Functions for setting the PHP error reporting level and controlling error handling with a custom error handler.
Syntax
// Sets or gets the error reporting level. error_reporting($level); // Sets a custom error handler. set_error_handler($callback, $error_levels); // Triggers a user-defined error. trigger_error($message, $error_level); // Changes a PHP configuration value. Also used to control error display. ini_set($option, $value);
Function List
| Function | Description |
|---|---|
| error_reporting($level) | Sets the error reporting level. If the argument is omitted, returns the current level. Use E_ALL to report all errors. |
| set_error_handler($callback, $error_levels) | Registers a custom function to replace PHP's default error handler. The callback receives four arguments: $errno, $errstr, $errfile, and $errline. |
| trigger_error($message, $error_level) | Deliberately triggers an error from your code. The second argument accepts E_USER_NOTICE, E_USER_WARNING, or E_USER_ERROR. |
| ini_set($option, $value) | Changes a PHP configuration value at runtime. Commonly used to control display_errors and log_errors. |
Common Error Level Constants
| Constant | Description |
|---|---|
| E_ALL | Reports all errors and warnings. Recommended for development environments. |
| E_ERROR | Fatal runtime errors. Script execution stops. |
| E_WARNING | Runtime warnings. Script execution continues. |
| E_NOTICE | Runtime notices, such as accessing an undefined variable. |
| E_DEPRECATED | Warnings about code that will stop working in a future version. |
| E_STRICT | Suggestions about code compatibility and future issues. Merged into E_ALL in PHP 8.0. |
Sample Code
<?php
// Report all errors. Required setting for development environments.
error_reporting(E_ALL);
ini_set('display_errors', '1'); // Display errors on screen.
// In production, hide errors from the screen and log them instead.
error_reporting(E_ALL);
ini_set('display_errors', '0');
ini_set('log_errors', '1');
ini_set('error_log', '/var/log/php_errors.log');
// Get the current error reporting level.
$current = error_reporting();
echo $current; // Outputs '32767' when set to E_ALL.
// Set a custom error handler.
set_error_handler(function ($errno, $errstr, $errfile, $errline) {
$date = date('Y-m-d H:i:s');
$log = "[$date] Error($errno): $errstr — $errfile:$errline\n";
error_log($log, 3, '/var/log/app_errors.log');
return true; // Returning 'true' suppresses PHP's built-in error handler.
});
// Trigger a user-defined error with trigger_error().
function divide($a, $b) {
if ($b == 0) {
trigger_error("Division by zero attempted", E_USER_WARNING);
return false;
}
return $a / $b;
}
$result = divide(10, 0); // The custom handler logs the warning.
// Report all errors except notices and deprecation warnings.
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
Notes
error_reporting() controls which error levels PHP reports. In development, set E_ALL to display all errors. In production, disable display_errors and log errors instead. Displaying errors in production risks exposing file paths and internal application details.
set_error_handler() lets you fully customize what happens when an error occurs — logging to a file, sending email notifications, showing a custom error page, and more. Note that fatal errors such as E_ERROR and E_PARSE cannot be caught by a custom handler; use register_shutdown_function() to handle those.
For structured error handling with exceptions, see try / catch.
If you find any errors or copyright issues, please contact us.