# `error_clear_last()`

> Section in “New and Extended Functionality › Error Handling” of the book [PHP 7 Explained](https://php7explained.com/) by Sebastian Bergmann and Stefan Priebsch, published by thePHP.cc. The book covers PHP 7.0 to 7.4 and was last updated in November 2020. HTML version: <https://php7explained.com/new-and-extended-functionality/error-handling/error_clear_last/>

Unknown to many developers, PHP provides an API to get the last error that occurred even without the need to mess with the PHP configuration and enable tracking of errors. This is a rather handy feature, given that it can be used in a shutdown function to check if any unhandled errors occurred. It also allows for a controlled shutdown just in case.

``` php
register_shutdown_function(
    function() {
        $error = error_get_last();
        if ($error !== null) {
            var_dump($error);
        }
    }
);
```

The only downside to this approach is that with PHP 5 there was no way to clear the error entry once it got handled. This reduced the usefulness of this feature quite dramatically, at least, when other areas of the code needed to deal with error handling as well.

With PHP 7, the new function `error_clear_last()` got introduced which fixes that oversight.

``` php
@$x = $undefined;
var_dump(error_get_last());

error_clear_last();

var_dump(error_get_last());
```

If executed, the above example will output an `array` with details of the suppressed notice for using an undefined variable. After clearing the error, the following call returns `null`.

---

Previous: [Better syslog support with error_log](https://php7explained.com/new-and-extended-functionality/error-handling/better-syslog-support-with-error_log/index.html.md) | Next: [Changed E_STRICT Notices](https://php7explained.com/new-and-extended-functionality/error-handling/changed-e_strict-notices/index.html.md)
