PHP error_clear_last() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Error

Definition

In PHP, error_clear_last() erases the record of the most recent error, so that error_get_last() returns null again.

PHP quietly remembers the last error that happened, even when you hide it with the @ operator. That is handy, but an old error can confuse your checks. Calling error_clear_last() before a risky operation gives you a clean slate, so any error you find afterwards is definitely new.

Syntax

Call error_clear_last() with the values you need. Start with this line.

PHP
error_clear_last();
CallWhat it does
error_clear_last()Clears the stored last error. It takes no arguments.
error_clear_last(); $x = @file_get_contents($file);Clears old errors first, then runs a risky call with a clean slate.
error_clear_last(); var_dump(error_get_last());Shows NULL because the last error was just cleared.

error_clear_last() only clears what error_get_last() reports. It does not undo the error, remove log entries, or change error handlers.

Library declarations
PHP
error_clear_last(): void

Parameters

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
voidNo value is returned. The last error record is simply emptied.

PHP Examples

Example 1 — Clearing the Last Error

We create a quiet warning, read it, clear it, and then read again.

PHP
<?php
@trigger_error("Disk almost full", E_USER_WARNING);

$before = error_get_last();
echo "Before clear: ", $before['message'], PHP_EOL;

error_clear_last();

$after = error_get_last();
echo "After clear: ", $after === null ? 'none' : $after['message'], PHP_EOL;

How It Works

  1. trigger_error() raises a user warning, and the @ operator hides it from the screen.
  2. PHP still remembers the warning, so error_get_last() returns an array with the message.
  3. error_clear_last() erases that stored record.
  4. The second error_get_last() returns null, so the script prints none.

Example 2 — Clean Slate Before a Risky Call

An old error is still stored. Clearing it first makes sure the message you read belongs to the file operation.

PHP
<?php
@trigger_error("Old problem", E_USER_NOTICE);

error_clear_last();

$content = @file_get_contents('does-not-exist.txt');
$error = error_get_last();

if ($content === false && $error !== null) {
    echo "Failed: ", $error['message'], PHP_EOL;
}

How It Works

  1. An older notice is stored, which could be mistaken for a new error.
  2. error_clear_last() removes it before we try to read the file.
  3. file_get_contents() fails because the file does not exist, and the @ operator hides the warning.
  4. error_get_last() now returns the warning from the file read, which is the one we care about.

Example 3 — Safe to Call Anytime

error_clear_last() returns nothing and is safe to call even when there is no error to clear.

PHP
<?php
var_dump(error_get_last());
var_dump(error_clear_last());

@trigger_error("First", E_USER_NOTICE);
@trigger_error("Second", E_USER_NOTICE);
echo error_get_last()['message'], PHP_EOL;

error_clear_last();
error_clear_last();
var_dump(error_get_last());

How It Works

  1. At the start no error has happened, so error_get_last() returns NULL.
  2. var_dump(error_clear_last()) prints NULL because the function returns void.
  3. Only the newest error is remembered, so after two notices the message is Second.
  4. Clearing twice causes no problem, and error_get_last() is NULL again.

Frequently Asked Questions

It clears the information about the most recent error, so error_get_last() returns null until a new error happens.
PHP keeps the last error even if it was hidden or happened a long time ago. If you check error_get_last() after a risky call, you could read an old error by mistake. Clearing it first prevents that.
No. It only resets what error_get_last() reports. Messages that were already written to a log file or to the screen stay there.
No. You simply call error_clear_last() with empty parentheses. It returns nothing.
error_clear_last() forgets the last error that happened. restore_error_handler() changes which function handles future errors. They solve different problems.

Next: error_get_last()

error_get_last() returns information about the most recent error that happened in your scr.

error_get_last() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful