PHP Error Handling restore_error_handler() Function



The PHP Error Handling restore_error_handler() function is used to recover the previous error handlers. This function removes any custom error handlers set with set_error_handler(). It restores PHP's previous error-handling functionality. This is useful if you want to stop using your own error handler. It helps to keep your software clean and under control.

Any custom error handler that was set using set_error_handler() will be deleted when restore_error_handler() is called, and the error handler that was in place before set_error_handler() was used will be used again.

Syntax

Below is the syntax of the PHP Error Handling restore_error_handler() function −

bool restore_error_handler()

Parameters

This function does not accepts any parameter.

Return Value

The restore_error_handler() function returns TRUE on success. And FALSE on failure.

PHP Version

First introduced in core PHP 4.0.1, the restore_error_handler() function continues to function easily in PHP 5, PHP 7, and PHP 8.

Example 1

Here is the basic example of the PHP Error Handling restore_error_handler() function to return the previous error handler after the custom error handler has done its task. This prevents subsequent code execution from maintaining the custom error handler.

<?php
   function unserialize_handler($errno, $errstr) {
      echo "Invalid hello value.\n";
   }

   $hello = 'abc';
   set_error_handler('unserialize_handler');

   $original = unserialize($hello);
   restore_error_handler();
?>

Output

Here is the outcome of the following code −

Invalid hello value.

Example 2

This program demonstrates how to switch between two custom error handlers, restore the default handler in the middle, and use the restore_error_handler() function to handle the transition.

<?php
   // First custom error handler
   function firstErrorHandler($errno, $errstr) {
      echo "First Handler: [$errno] $errstr\n";
   }

   // Second custom error handler
   function secondErrorHandler($errno, $errstr) {
      echo "Second Handler: [$errno] $errstr\n";
   }

   // Set the first error handler
   set_error_handler("firstErrorHandler");

   // Trigger an error
   echo $undefined_variable;

   // Restore default handler
   restore_error_handler();
   set_error_handler("secondErrorHandler");

   // Trigger another error
   echo $undefined_variable_2;
?> 

Output

This will generate the below output −

First Handler: [2] Undefined variable $undefined_variable
Second Handler: [2] Undefined variable $undefined_variable_2

Example 3

Now in the below code we will use restore_error_handler() function and log errors to a file with the help of a custom error handler and show the restoration of PHP's default error handler after that.

<?php
   // Custom error handler 
   function loggingErrorHandler($errno, $errstr) {
      error_log("Error [$errno]: $errstr", 3, "errors.log");
      echo "Error logged to file.\n";
   }

   // Set the logging error handler
   set_error_handler("loggingErrorHandler");

   // Trigger an error
   echo $undefined_variable;

   // Restore the default error handler
   restore_error_handler();

   // Trigger another error (handled by default handler)
   echo $another_undefined_variable;
?> 

Output

This will create the below output −

Error logged to file.

Example 4

In the following example, we are using the restore_error_handler() function to to conditionally handle errors and restores the default handler for unhandled error types and showing flexible error management.

<?php
   // Conditional custom error handler
   function conditionalErrorHandler($errno, $errstr) {
      if ($errno === E_NOTICE) {
         echo "Handled Notice: $errstr\n";
      } else {
         // Restore default handler for other errors
         restore_error_handler();
         trigger_error($errstr, $errno);
      }
   }

   // Set the conditional error handler
   set_error_handler("conditionalErrorHandler");

   // Trigger a notice
   echo $undefined_variable;

   // Trigger a warning 
   trigger_error("This is a warning!", E_USER_WARNING);  
?> 

Output

Following is the output of the above code −

Handled Notice: Undefined variable: undefined_variable
PHP Warning:  This is a warning! in /Users/abc/Desktop/PHP/PhpProjects/index.php on line 15
php_function_reference.htm
Advertisements