Please wait

Error Handler Functions

Setting a custom error handler in PHP offers a heightened level of control and flexibility over the default error-handling mechanism. There are various reasons for using a custom error handler:

  • Instead of showing a generic error message, you can define tailored responses for different error types. This can provide users with friendlier or more helpful feedback.
  • While PHP can log errors out-of-the-box, a custom handler allows you to log additional data or integrate with third-party logging tools, helping in more detailed diagnostics.
  • Rather than exposing sensitive system information with default error messages (especially in a production environment), custom error handlers can show generic messages while logging the detailed error internally.

Setting an Error Handler

Creating a custom error handler in PHP involves defining a custom function and then telling PHP to use this function for handling errors. Let's walk through the process.

The first step is to use the set_error_handler() function. This function accepts the name of a function to call when an error occurs.

set_error_handler("customErrorHandler");

In this example, we're telling PHP to run a function called customErrorHandler. This function can accept up to five parameters (though not all are mandatory)

Parameter NameRequiredDescription
$errnoYesSpecifies the error type. This parameter can have values like E_ERROR, E_WARNING, E_NOTICE, etc.
$errstrYesDescribes the error in the form of a string message.
$errfileNoSpecifies the filename in which the error occurred.
$errlineNoSpecifies the line number in the file where the error occurred.
$errcontextNoAn array that points to the active symbol table at the point the error occurred. Rarely used in practice.

Here's an example of our customErrorHandler function.

function customErrorHandler($errno, $errstr, $errfile, $errline) {
  echo "<b>Custom Error:</b> [$errno] $errstr<br>";
  echo " Error on line $errline in $errfile<br>";
  echo "Ending Script";
  die();  // End the script
}

For demonstration purposes, we can trigger an error to see our custom error handler in action.

echo $undefinedVariable;  // This will cause an error

When the error is triggered, instead of the default PHP error message, you will see the output from the customErrorHandler function.

<b>Custom Error:</b> [2] Undefined variable $undefinedVariable<br> Error on line 15 in script.php<br>Ending Script

Things to Note

  • The custom error handler will not handle fatal errors. Fatal errors will continue to be handled by PHP's default error mechanism.
  • You can further extend the custom error handler to log errors to a file or a database, send email notifications, or integrate with monitoring services.
  • It's a good practice to avoid displaying detailed error messages on a production site for security reasons. Instead, log the detailed errors and display a generic message to the user.

Key Takeaways

  • Custom error handlers give developers the flexibility to define how errors are processed and presented, allowing for more tailored responses.
  • The custom error handler function can accept up to five parameters: $errno (error type), $errstr (error message), $errfile (error file), $errline (error line), and $errcontext (error context).
  • Use the set_error_handler() function to designate a function as the custom error handler.
  • Custom error handlers can't handle fatal errors. These are still managed by PHP's default mechanism.

Comments

Please read this before commenting