PHP error_get_last() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Error

Definition

In PHP, error_get_last() returns information about the most recent error that happened in your script, or null if there has not been one.

It is useful when an error was hidden with the @ operator, or when you want to inspect what went wrong inside a shutdown function. The result is an array with the keys type, message, file and line, so you can see what happened and where.

Syntax

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

PHP
$error = error_get_last();
CallWhat it does
error_get_last()Returns an array describing the last error, or null if there was none.
error_get_last()['message']Reads just the error text. Check for null first.
error_get_last()['type']Reads the error level number, such as 2 for E_WARNING.

Only the very last error is kept, so a newer error replaces an older one. Use error_clear_last() to reset it. Even errors hidden with @ are recorded.

Library declarations
PHP
error_get_last(): ?array

Parameters

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
arrayAn associative array with the keys type (int), message (string), file (string) and line (int) describing the most recent error.
nullReturned when no error has happened yet, or when error_clear_last() was called.

PHP Examples

Example 1 — Reading a Hidden Error

The @ operator hides an error from the screen, but error_get_last() can still tell you about it.

PHP
<?php
@trigger_error("Cache folder is empty", E_USER_WARNING);

$error = error_get_last();

if ($error !== null) {
    echo "Keys: ", implode(', ', array_keys($error)), PHP_EOL;
    echo "Message: ", $error['message'], PHP_EOL;
    echo "Type: ", $error['type'], PHP_EOL;
    echo "Is user warning: ", $error['type'] === E_USER_WARNING ? 'yes' : 'no', PHP_EOL;
}

How It Works

  1. trigger_error() raises a user warning with our own message, and the @ operator keeps it quiet.
  2. error_get_last() returns an array with four keys: type, message, file and line.
  3. The type is 512, which is the number behind the E_USER_WARNING constant.
  4. Comparing the type with E_USER_WARNING is better than remembering the number.

Example 2 — Catching a Fatal Error in a Shutdown Function

A shutdown function runs when the script ends, even after an uncaught exception. There, error_get_last() tells you why the script stopped.

PHP
<?php
ini_set('display_errors', '0');
ini_set('log_errors', '0');

register_shutdown_function(function () {
    $error = error_get_last();

    if ($error !== null) {
        echo "Fatal error type: ", $error['type'], PHP_EOL;
        echo "Starts with: ", substr($error['message'], 0, 24), PHP_EOL;
    }
});

throw new Exception("Boom");

How It Works

  1. The two ini_set() lines hide the normal fatal error message so the output stays short.
  2. register_shutdown_function() registers code that runs when the script ends.
  3. The uncaught exception is reported as a fatal error of type 1, which is E_ERROR.
  4. Inside the shutdown function, error_get_last() returns that error, and we print the start of its message.

Example 3 — Checking a Failed File Read

Many built-in functions only return false when something fails. error_get_last() gives you the reason.

PHP
<?php
$data = @file_get_contents('missing-file.txt');

if ($data === false) {
    $error = error_get_last();
    echo "Could not read the file.", PHP_EOL;
    echo "Message: ", $error['message'], PHP_EOL;
    echo "Is warning: ", $error['type'] === E_WARNING ? 'yes' : 'no', PHP_EOL;
}

How It Works

  1. file_get_contents() cannot find the file, so it returns false and raises a warning.
  2. The @ operator hides the warning, but PHP still records it.
  3. We only call error_get_last() after confirming that the read failed.
  4. The message explains the reason, and the type matches E_WARNING.

Frequently Asked Questions

It returns an array with the keys type, message, file and line for the most recent error. If no error has happened, it returns null.
Yes. The @ operator only hides the error message. PHP still records the error, so error_get_last() can read it afterwards.
Register a function with register_shutdown_function(), and call error_get_last() inside it. If the result is not null and the type is a fatal one such as E_ERROR, the script stopped because of that error.
Either no error has happened yet, or error_clear_last() was called after the last one. A custom error handler that returns true also stops PHP from recording the error.
Call error_clear_last(). After that, error_get_last() returns null until a new error occurs.

Next: error_log()

error_log() sends a message to a log. Depending on the settings, the log can be the server.

error_log() 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