Motivation
The default error handling in PHP is very simple. An error message with filename, line number and a message describing the error is sent to the browser.
What if you want to save this error also to a log file ? or catch warning\error\exception ?
Creating Custom Error Handler
Creating a custom error handler is quite simple. We simply create a special function that can be called when an error occurs in PHP.
This function must be able to handle a minimum of two parameters (error level and error message) but can accept up to five parameters (optionally: file, line-number, and the error context):
Syntax
error_function(error_level,error_message,
error_file,error_line,error_context)
| Parameter | Description |
|---|---|
| error_level | Required. Specifies the error report level for the user-defined error. Must be a value number. See table below for possible error report levels |
| error_message | Required. Specifies the error message for the user-defined error |
| error_file | Optional. Specifies the filename in which the error occurred |
| error_line | Optional. Specifies the line number in which the error occurred |
| error_context | Optional. Specifies an array containing every variable, and their values, in use when the error occurred |
Error Report levels
These error report levels are the different types of error the user-defined error handler can be used for:
| Value | Constant | Description |
|---|---|---|
| 2 | E_WARNING | Non-fatal run-time errors. Execution of the script is not halted |
| 8 | E_NOTICE | Run-time notices. The script found something that might be an error, but could also happen when running a script normally |
| 256 | E_USER_ERROR | Fatal user-generated error. This is like an E_ERROR set by the programmer using the PHP function trigger_error() |
| 512 | E_USER_WARNING | Non-fatal user-generated warning. This is like an E_WARNING set by the programmer using the PHP function trigger_error() |
| 1024 | E_USER_NOTICE | User-generated notice. This is like an E_NOTICE set by the programmer using the PHP function trigger_error() |
| 4096 | E_RECOVERABLE_ERROR | Catchable fatal error. This is like an E_ERROR but can be caught by a user defined handle (see also set_error_handler()) |
| 8191 | E_ALL | All errors and warnings (E_STRICT became a part of E_ALL in PHP 5.4) |
Now lets create a function to handle errors:
function customError($errno, $errstr) {
echo "<b>Error:</b> [$errno] $errstr<br>";
echo "Ending Script";
die();
}
The code above is a simple error handling function. When it is triggered, it gets the error level and an error message. It then outputs the error level and message and terminates the script.
Now that we have created an error handling function we need to decide when it should be triggered.
Set Error Handler
function customError($errno, $errstr,$error_file,$error_line)
{
echo "<b>Error:</b> $error_file $error_line [$errno] $errstr";
}
//set error handler
set_error_handler("customError");
//trigger error
echo($test);
this output :
Error: E:\nathan\PHP\VS\PHPWebProject2\PHPWebProject2\index.php 17 [8] Undefined variable: test
Using log file \ Email
you can send the error to a log_file using error_log
The error_log() function sends an error message to a log, to a file, or to a mail account.
error_log(message,type,destination,headers);
| Parameter | Description |
|---|---|
| message | Required. Specifies the error message to log |
| type | Optional. Specifies where the error message should go. Possible values:
|
| destination | Optional. Specifies the destination of the error message. This value depends on the value of thetype parameter |
| headers | Optional. Only used if the type parameter is set to 1. Specifies additional headers, like From, Cc, and Bcc. Multiple headers should be separated with a CRLF (\r\n) |
function customError($errno, $errstr,$error_file,$error_line)
{
// --- error to display
echo "<b>Error:</b> [$errno] $errstr";
// --- error to PHP system log
$error = $error_file." ".$error_line." [".$errno."] ". $errstr;
error_log($error,0); // send to php error file
error_log($error,1,"natankrasney@gmail.com","From: webmaster@example.com");
}
//set error handler
set_error_handler("customError");
//trigger error
echo($test);
this will output to the browser :
Exception Handling
the set_exception_handler() function sets a user-defined function to handle all uncaught exceptions.
<?php
function myException($exception) {
echo "<b>Exception:</b> " . $exception->getMessage();
}
set_exception_handler('myException');
throw new Exception('Uncaught Exception occurred');
?>
The output of the code above should be something like this:
Exception: Uncaught Exception occurred
it is possible to log the exception as follows :
function customError($errno, $errstr,$error_file,$error_line)
{
// --- error to display
echo "<b>Error:</b> [$errno] $errstr";
// --- error to PHP system log
$error = $error_file." ".$error_line." [".$errno."] ". $errstr;
// send to php error file
error_log($error,0); // send to php error file
// send to php error to e-mail but need configuration
//error_log($error,1,"natankrasney@gmail.com","From: webmaster@example.com");
}
function exception_handler($exception) {
trigger_error( "Uncaught exception: ".$exception->getMessage()."\n");
}
set_exception_handler('exception_handler');
//set error handler
set_error_handler("customError");
$error = 'Always throw this error';
throw new Exception($error);
//trigger error
echo($test);
References
Nathan





