By default, PHP uses its built-in error handling mechanism. To override it and use a custom built error handler, use set_error_handler.
set_error_handler("myErrorHandler");
myErrorHandler is a custom built function.
function errorHandler($level,$message,$file,$line){
switch($level){
case 1:
$constant="PHP_ERROR";
break;
case 2:
$constant="PHP_WARNING";
break;
case 8:
$constant="PHP_NOTICE";
break;
case 256:
$constant="ERROR";
break;
case 512:
$constant="WARNING";
break;
case 1024:
$constant="NOTICE";
break;
default:
$constant="undefined error";
break;
}
logToFile("[[$constant]] \"$message\" due to file $file, on line $line");
}
The following error levels are a few possibilities that could be sent towards your custom error handler.
| Value | Constant | Description |
|---|---|---|
| 1 | E_ERROR | A fatal run-time error. Execution of the script is stopped |
| 2 | E_WARNING | A non-fatal run-time error. Execution of the script is not stopped |
| 8 | E_NOTICE | A run-time notice. The script found something that might be an error, but could also happen when running a script normally |
| 256 | E_USER_ERROR | A fatal user-generated error. This is like an E_ERROR, except it is generated by the PHP script using the function trigger_error() |
| 512 | E_USER_WARNING | A non-fatal user-generated warning. This is like an E_WARNING, except it is generated by the PHP script using the function trigger_error() |
| 1024 | E_USER_NOTICE | A user-generated notice. This is like an E_NOTICE, except it is generated by the PHP script using the function trigger_error() |
| 2048 | E_STRICT | Not strictly an error. |
| 8191 | E_ALL | All errors and warnings (E_STRICT became a part of E_ALL in PHP 5.4) |