Skip to content

set_error_handler()

PHP is an open-source scripting language widely used for web development. Effective error handling is crucial for building reliable applications, and PHP provides robust mechanisms to manage them. This article covers PHP's error handling capabilities, focusing on the set_error_handler() function.

Understanding PHP's Error Handling Capabilities

Error handling is an essential aspect of any programming language that allows developers to handle errors or exceptions that might occur in their application. In PHP, errors can occur due to various reasons, such as syntax errors, runtime errors, or logical errors. To handle these errors efficiently, PHP provides a set of functions that can be used to catch and manage errors effectively.

PHP's set_error_handler() Function

One of the most important functions that PHP provides for error handling is set_error_handler(). This function sets a user-defined error handler that will be called whenever a specified error occurs. The syntax is as follows:

Syntax of PHP set_error_handler() function

php
callable|false set_error_handler ( callable $error_handler [, int $error_types = E_ALL ] )

The set_error_handler() function takes two parameters. The first parameter is the user-defined error handler function. The second parameter is optional and specifies which error types the handler should respond to. By default, it handles all errors.

Note: set_error_handler() cannot catch fatal errors such as E_ERROR, E_PARSE, E_CORE_ERROR, or E_COMPILE_ERROR. These errors will still terminate the script immediately.

Using the set_error_handler() Function

To use the set_error_handler() function, you need to define a custom error handler function. This function should accept four parameters: the error type, error message, file name, and line number where the error occurred. Once defined, you can pass it to set_error_handler() to register it as the default error handler for your PHP application.

Example Code

Example of PHP set_error_handler() function

php
<?php

function custom_error_handler($errno, $errstr, $errfile, $errline)
{
    echo "<b>Error:</b> [$errno] $errstr<br />";
    echo "Error on line $errline in $errfile<br />";
}

set_error_handler("custom_error_handler");

// Trigger a user-generated warning to demonstrate the handler
trigger_error("This is a test warning", E_USER_WARNING);

// Restore the default error handler
restore_error_handler();

In the example above, we define a custom error handler and register it using set_error_handler(). The trigger_error() call simulates a warning to demonstrate the handler in action. Finally, restore_error_handler() reverts the error handling back to PHP's default behavior.

Conclusion

Error handling is a fundamental aspect of programming, and PHP offers robust tools to manage it effectively. The set_error_handler() function allows developers to intercept and process errors using custom logic, leading to more robust and reliable applications.

Practice

In PHP, what does the set_error_handler() function do?

Do you find this helpful?

Dual-run preview — compare with live Symfony routes.