The set_error_handler() function in PHP sets a user-defined function to handle errors that occur during script execution. It allows developers to customize error handling, log error messages, and respond to specific error levels.
- It can handle errors such as warnings, notices, and user-generated errors.
- It accepts a callback function and an optional error-level mask.
- The custom handler can receive the error type, message, filename, and line number.
- It cannot handle certain errors, including E_ERROR, E_PARSE, E_CORE_ERROR, and E_COMPILE_ERROR.
Syntax:
set_error_handler(?callable $callback, int $error_levels = E_ALL): ?callableParameters:
- $callback: The function to call when a matching error occurs. Pass null to remove the active custom handler.
- $error_levels: An optional bitmask specifying which error levels the handler should process. The default is E_ALL.
Return value: Returns the previously defined error handler, or null if PHP's built-in handler was active.
Example 1: Using set_error_handler()
The following example defines a custom error handler and uses trigger_error() to generate a user-level warning.
<?php
function customErrorHandler($errno, $errstr, $errfile, $errline) {
echo "Error: $errstr\n";
echo "File: $errfile\n";
echo "Line: $errline\n";
}
set_error_handler("customErrorHandler");
trigger_error("Invalid input provided", E_USER_WARNING);
?>
Note: The filename and line number depend on where the error occurs.
- customErrorHandler() defines how the error should be handled.
- set_error_handler() registers the function as the custom error handler.
- trigger_error() generates a user-level warning.
- PHP passes the error details to the registered handler.
Example 2: Handling Specific Error Levels
The $error_levels parameter allows developers to register a handler for specific error types.
<?php
function customErrorHandler($errno, $errstr) {
echo "Custom handler: $errstr";
}
set_error_handler("customErrorHandler", E_USER_WARNING);
trigger_error("This is a warning", E_USER_WARNING);
?>
Here, the custom handler is registered for E_USER_WARNING. Other error levels are not handled by this callback and generally fall back to PHP's standard error handler.
Example 3: Restoring the Previous Error Handler
The restore_error_handler() function restores the error handler that was active before the most recent set_error_handler() call.
<?php
function customErrorHandler($errno, $errstr) {
echo "Custom handler: $errstr\n";
}
set_error_handler("customErrorHandler");
trigger_error("First warning", E_USER_WARNING);
restore_error_handler();
trigger_error("Second warning", E_USER_WARNING);
?>
The exact format of the second warning depends on the PHP configuration and execution environment.
The first warning is processed by the custom handler. After restore_error_handler() is called, the previous handler is restored, so the second warning uses PHP's standard error handling.
Best Practices
- Use a custom error handler to centralize error handling and logging.
- Specify appropriate error levels when registering the handler to handle only relevant errors.
- Check error_reporting() when necessary to respect the current error reporting configuration.
- Avoid displaying sensitive error details to end users in production environments.
- Log errors with relevant details, such as the error message, filename, and line number.
- Use restore_error_handler() when the custom handler is no longer needed.