Thursday, July 9, 2026
HomeLanguagesPHP error_clear_last() Function

PHP error_clear_last() Function

The error_clear_last() function is an inbuilt function in PHP that is utilized to remove the most recent error.

Syntax:

error_clear_last(): void

Parameter: This function does not accept any parameters.

Return Value: The most recent error will be cleared & making it impossible to retrieve that error by using the error_get_last() function by this function.

Example 1: The following code demonstrates the error_clear_last() function.

PHP




<?php
  
echo $age;
  
$error = error_get_last();
var_dump($error);
  
// Clear the last error
error_clear_last();
  
// Check the last error again
$error = error_get_last();
var_dump($error);
?>


Output:

array(4) {
    ["type"]=>  int(2)
    ["message"]=> string(23) "Undefined variable $age"
    ["file"]=> string(51) "/home/dachman/Desktop/Articles/GFG/Method/index.php"
    ["line"]=> int(3)
}
NULL 

Example 2: The following code demonstrates the error_clear_last() function.

PHP




<?php
function custom_error_handler($errno, 
$errstr, $errfile, $errline) {
    echo "Error: $errstr\n";
}
  
set_error_handler("custom_error_handler");
  
// Generate an error
echo $age;
  
// Clear the last error
error_clear_last();
  
// Generate another error
echo $age;
  
?>


Output:

Error: Undefined variable $age
Error: Undefined variable $age

Reference: https://www.php.net/manual/en/function.error-clear-last.php

RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32519 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6901 POSTS0 COMMENTS
Nicole Veronica
12017 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12111 POSTS0 COMMENTS
Shaida Kate Naidoo
7020 POSTS0 COMMENTS
Ted Musemwa
7263 POSTS0 COMMENTS
Thapelo Manthata
6978 POSTS0 COMMENTS
Umr Jansen
6968 POSTS0 COMMENTS