How to Define a Custom Error Handler in WebSmart PHP

Error messages are an important part of the WebSmart PHP development process, providing valuable information that you can use to fix and improve your scripts. If your script encounters an error, PHP normally displays the error in the browser and, if the error is fatal, stops execution of the script.

But you can override this default functionality, replacing it with your own function that handles errors at runtime. This is a simple two-step process that involves defining your error-handling function and setting it to handle errors in your script with set_error_handler().

Defining a Custom Error Handler
You can define an error-handling function that accepts between two and five parameters. The first two parameters, which your function is required to accept, are the error level and a message describing the error. Your custom function can have these parameters:

customErrorHandler(int $errorType, string $errorDesc, string $fileName, 
                   int $lineNumber, array $errContext)
Parameter Description
errorType The level of the error raised. This value is an integer. See this table of PHP’s built-in error levels. Required.
errorDesc The error message. Required.
fileName The filename in which the error occured. Optional.
lineNumber The line number of the error. Optional.
errorContext An array of each variable and their values at the time of the error. Optional.

You can do anything you want in the error-handling function, but here are some ideas:

  • Store the error to an IFS file or database.
  • Email warnings or errors to developers, so they are immediately notified of the problem.
  • Format the error message with the date and time that it occurred.
  • Handle the error differently depending on the error level.

The next step is setting up your script to trigger the error-handling function when your script has an error.

Setting the Error-Handling Function to Handle Errors in Your Script
You can override PHP’s default error handler and register your own custom error handler by using PHP’s built-in function set_error_handler(). In the following example, we make our function named ‘customErrorHandler’ be the error handler for the duration of the script:

// register errors to a custom error handler
set_error_handler("customErrorHandler", E_ALL)

This function accepts two parameters:

Parameter Description
errorHandler Sets the name of your error-handling function. This function runs at the time of the error.
errorTypes Sets the level of error that will trigger the function. Note that set_error_handler() doesn’t work for fatal errors (E_ERROR).

Example Code
The example below illustrates how to build a custom error handler to format errors and print them to a browser and log file. Note that, in this sample code, error_log is a built-in PHP function that lets you log errors to the specified file.

// custom error handler
function customErrorHandler($errorType, $errorDesc, $fileName, $lineNumber)
     $error = "$errorDesc, FILE: $fileName, LINE: $lineNumber";
     echo $error;

     // let's add the timestamp and newline (same format as in php.log)
     $date = new DateTime();
     $error = "[". $date->format('d-M-Y H:i:s') . "]  " . $error ."\n";

     // log it to a custom file
     error_log($error, 3, "/www/zendsvr/htdocs/example/errors.log");

// register errors to a custom error handler
set_error_handler("customErrorHandler", E_ALL);

$y = $t[1]; // Notice
$x = 1/0; // Warning
class Strict 
    public function test() {} 
Strict::test(); // Strict

If we run this code, the page displays the following error messages:

Undefined variable: t, FILE: /www/websmart/htdocs/wsphp/testtmc3.php, LINE: 19
Division by zero, FILE: /www/websmart/htdocs/wsphp/testtmc3.php, LINE: 21

Online Reference:

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>