error_clear_last

Clears the most recent error so that error_get_last() reports null again.

Signature
error_clear_last(): void
  • Added in PHP 7.0.0

Return value

voidNo value is returned.

error_clear_last() resets PHP's single slot of “last error” information. After calling it, error_get_last() returns null until something new goes wrong.

Why you need it

PHP keeps exactly one last-error record for the whole request, and it is never cleared automatically. That makes error_get_last() unreliable on its own: if you suppress a call with @ and then inspect the last error, you may be reading a warning that happened three functions ago, in unrelated code. The reliable pattern is to clear first, run the risky call, then read:

error_clear_last();
$data = @unserialize($input);
if ($data === false && error_get_last() !== null) {
    // the failure really came from this call
}

This matters most around legacy functions that signal failure by emitting a warning and returning false — unserialize(), file_get_contents(), json_decode()'s older cousins, and most stream operations.

Scope

The record is global to the request, not per file or per function, so clearing it in one place affects every later inspection. Do not clear it in library code that someone else's error handling might depend on; keep the clear and the read tightly paired around a single call.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Isolate whether a @-suppressed unserialize() or stream call was the thing that actually failed
  • Reset error state between iterations of a loop that probes many optional inputs
  • Clean up after a deliberate probe so later diagnostics are not misled by it

Frequently asked questions

Does error_clear_last stop the error from being logged?

No. The error is written to the log and passed to any error handler before the record exists; clearing only removes the copy that error_get_last() would return.

Why do I need error_clear_last before error_get_last?

PHP keeps one last-error record for the entire request and never resets it, so without clearing you may read a warning from unrelated earlier code. Clear it immediately before the call you want to test.

Is error_clear_last available in PHP 5?

No, it was added in PHP 7.0.0. On PHP 5 you had to compare the whole error_get_last() array before and after a call to detect a new error.

More examples

Tell a real failure from a stale warning

The first call returns the decoded array. The second returns false because unserialize() emitted a warning that error_get_last() picks up. Without the error_clear_last() at the top, a warning from earlier in the request would make even valid input look like a failure.

Example · php
<?php
function safeUnserialize(string $raw): array|false {
    error_clear_last();
    $value = @unserialize($raw);
    if ($value === false && error_get_last() !== null) {
        return false;
    }
    return (array) $value;
}

var_dump(safeUnserialize('a:1:{i:0;s:2:"ok";}'));
var_dump(safeUnserialize('not serialized'));

Clearing inside a loop

Each iteration starts from a clean slate, so the verdict reflects only that iteration. Drop the clear and every input after "garbage" would be reported as failed, because the stale warning is never removed on its own.

Example · php
<?php
$inputs = ['a:0:{}', 'garbage', 'i:42;'];

foreach ($inputs as $raw) {
    error_clear_last();
    @unserialize($raw);
    $err = error_get_last();
    echo $raw, ' => ', $err === null ? 'ok' : 'failed', "\n";
}

Clearing does not touch exceptions

Exceptions live in a completely separate mechanism from PHP's error records, so the last-error slot stays empty either way. error_clear_last() only affects what the error system recorded, never what try/catch handled.

Example · php
<?php
try {
    throw new RuntimeException('thrown, not an error');
} catch (RuntimeException $e) {
    echo 'caught: ', $e->getMessage(), "\n";
}

var_dump(error_get_last());   // NULL - exceptions were never recorded
error_clear_last();
var_dump(error_get_last());

Discussion

  • Be the first to comment on this lesson.