If you desire a custom error handler, you have to use set_error_handler() because you need to let PHP know you want to use a custom error handler. If you followed up on this function, you have to define the actual function which has been set along the set_error_handler() callback. You are allowed to set different error handlers for different types of PHP errors.
The custom error handler can use at least 4 default arguments.
- Error number
- Error message
- Error file
- Error line
Three examples of custom defined error handlers for Notices, Warnings and Deprecated functions
<?php
// Set error handler E_NOTICE
set_error_handler('NoticeHandler', E_NOTICE);
// Warning - Notice handler
function NoticeHandler ($errnr, $errmsg, $errfile, $errline){
echo 'The function '. __FUNCTION__ .', picked up the notice and reports the following:'.
'<br />Error number: '. $errnr.
'<br />Error message: '. $errmsg.
'<br />Error line: '. $errline.
'<br />Error file: '. $errfile;
}
// Trigger the error
echo $undefined_variable;
// Set error handler E_WARNING
set_error_handler('WarningHandler', E_WARNING);
// Warning - Warning handler
function WarningHandler ($errnr, $errmsg, $errfile, $errline){
echo '<br /><br />The function '. __FUNCTION__ .', picked up the warning and reports the following:'.
'<br />Error number: '. $errnr.
'<br />Error message: '. $errmsg.
'<br />Error line: '. $errline.
'<br />Error file: '. $errfile;
}
file_get_contents('no/not/there.php');
// Set error handler E_DEPRECATED
set_error_handler('DeprecatedHandler', E_DEPRECATED);
// Warning - Error handler
function DeprecatedHandler ($errnr, $errmsg, $errfile, $errline){
echo '<br /><br />The function '. __FUNCTION__ .', picked up the error and reports the following:'.
'<br />Error number: '. $errnr.
'<br />Error message: '. $errmsg.
'<br />Error line: '. $errline.
'<br />Error file: '. $errfile;
}
echo session_register('Registered');
?>
The result
The function NoticeHandler, picked up the notice and reports the following:
Error number: 8
Error message: Undefined variable: undefined_variable
Error line: 30
Error file: C:\server\htdocs\tests\test.php
The function WarningHandler, picked up the warning and reports the following:
Error number: 2
Error message: file_get_contents(no/not/there.php) [function.file-get-contents]: failed to open stream: No such file or directory
Error line: 46
Error file: C:\server\htdocs\tests\test.php
The function DeprecatedHandler, picked up the error and reports the following:
Error number: 8192
Error message: Function session_register() is deprecated
Error line: 61
Error file: C:\server\htdocs\tests\test.php1
This list of error constants could be used for assigning a custom error handler to. Excluding the following list:
- E_ERROR
- E_PARSE
- E_CORE_ERROR
- E_CORE_WARNING
- E_COMPILE_ERROR
- E_COMPILE_WARNING
- E_STRICT
Mail this!
- Comments (0)
- PingBacks (0)
- TrackBacks (0)

» Latest comments