Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Custom Error Handlers in PHP

Introduction

Error handling is a crucial part of any programming language as it helps developers manage and respond to errors gracefully. In PHP, custom error handlers allow developers to define how different types of errors should be handled, providing greater control over the error management process.

Setting Up a Custom Error Handler

To set up a custom error handler in PHP, you need to define a function that will handle errors and then use the set_error_handler() function to register it.

Example:

<?php
function customErrorHandler($errno, $errstr, $errfile, $errline) {
    echo "<b>Error number:</b> $errno <br>";
    echo "<b>Error message:</b> $errstr <br>";
    echo "<b>Error file:</b> $errfile <br>";
    echo "<b>Error line:</b> $errline <br>";
}

set_error_handler("customErrorHandler");

// Trigger an error
echo($test);
?>
                

Error number: 8
Error message: Undefined variable: test
Error file: /path/to/your/script.php
Error line: 12

Handling Different Error Levels

PHP provides several predefined error levels. You can use these levels to handle different types of errors in your custom error handler.

  • E_WARNING
  • E_NOTICE
  • E_USER_ERROR
  • E_USER_WARNING
  • E_USER_NOTICE

For example, to handle warnings differently, you can modify the custom error handler function:

<?php
function customErrorHandler($errno, $errstr, $errfile, $errline) {
    switch ($errno) {
        case E_WARNING:
            echo "<b>Warning:</b> $errstr in $errfile on line $errline <br>";
            break;
        case E_NOTICE:
            echo "<b>Notice:</b> $errstr in $errfile on line $errline <br>";
            break;
        default:
            echo "<b>Error number:</b> $errno <br>";
            echo "<b>Error message:</b> $errstr <br>";
            echo "<b>Error file:</b> $errfile <br>";
            echo "<b>Error line:</b> $errline <br>";
            break;
    }
}

set_error_handler("customErrorHandler");

// Trigger a warning
echo($test);
?>
                

Logging Errors

Instead of or in addition to displaying errors, you might want to log them to a file for later review. This can be easily achieved by modifying the custom error handler to write to a log file.

<?php
function customErrorHandler($errno, $errstr, $errfile, $errline) {
    $logMessage = "[$errno] $errstr in $errfile on line $errline" . PHP_EOL;
    error_log($logMessage, 3, "errors.log");
    echo "An error occurred. Please check the log file.";
}

set_error_handler("customErrorHandler");

// Trigger an error
echo($test);
?>
                

Restoring the Default Error Handler

If you need to restore the default PHP error handler at any point, you can use the restore_error_handler() function.

<?php
function customErrorHandler($errno, $errstr, $errfile, $errline) {
    echo "<b>Error number:</b> $errno <br>";
    echo "<b>Error message:</b> $errstr <br>";
    echo "<b>Error file:</b> $errfile <br>";
    echo "<b>Error line:</b> $errline <br>";
}

set_error_handler("customErrorHandler");

// Trigger an error
echo($test);

// Restore the default error handler
restore_error_handler();

// Trigger another error
echo($anotherTest);
?>
                

Conclusion

Custom error handlers in PHP provide a flexible way to manage errors in your applications. By defining your own error handling functions, you can control how errors are displayed, logged, and managed, leading to better error management and a more robust application.