The set_error_handler()
function in PHP allows you to define a custom error handler function, which will be called whenever an error occurs in your PHP script. This can be useful for suppressing certain types of errors, logging errors to a file, or sending notifications to an administrator.
Here’s an example of how you could use set_error_handler()
to define a custom error handler:
function customErrorHandler($errno, $errstr, $errfile, $errline) {
// custom error handling code
error_log("[$errno] $errstr in $errfile on line $errline");
}
set_error_handler("customErrorHandler");
In this example, we define a custom error handler function customErrorHandler()
which takes four parameters: $errno
, $errstr
, $errfile
, and $errline
. These parameters contain information about the error that occurred, including the error number, error message, file name, and line number.
We then use the set_error_handler()
function to register our custom error handler, and pass the name of the custom error handler function as an argument.
With this custom error handler in place, any errors that occur in your PHP script will be logged using the error_log()
function, and will not be displayed to the user.