error_get_last()
PHP is a popular server-side programming language used to build dynamic websites and web applications. Error handling is an important part of PHP programming
Error handling is an important part of PHP programming, because errors can occur at any point during execution. The error_get_last() function is a built-in PHP function that lets you inspect the most recent error PHP recorded — its type, message, file, and line — so you can log it or react to it in code. This chapter explains what the function returns, when it returns null, the kinds of errors it does and does not capture, and how to use it correctly in modern PHP.
What error_get_last() returns
error_get_last() takes no arguments and returns information about the last error that PHP raised during the current request. The return value is one of two things:
- An associative array with four keys, if an error has occurred.
null, if no error has been recorded yet.
The array has the following shape:
[
'type' => 2, // int: the error level (E_WARNING here)
'message' => 'fopen(...): ...', // string: the error description
'file' => '/path/to/script.php', // string: file where the error happened
'line' => 12, // int: line number where it happened
]The type element is an integer matching one of PHP's error-level constants — for example E_WARNING (2), E_NOTICE (8), E_USER_WARNING (512), or E_DEPRECATED. See the PHP error reference for the full list.
What it does and does not capture
This is the part people get wrong, so it is worth being precise:
- It does capture reported errors, warnings, notices, and deprecations — anything that flows through PHP's normal error mechanism, including those raised with
trigger_error(). - It does not capture exceptions. In modern PHP (7.0+), many failures throw exceptions instead of raising errors — for example, division and modulo by zero throw
DivisionByZeroError, not a warning. Those must be handled withtry/catch, anderror_get_last()will not see them. - It only sees errors that are actually reported. If an error level is filtered out by
error_reporting(), it will not appear. Enableerror_reporting(E_ALL)while debugging so nothing is silently dropped.
Because of this, error_get_last() is most useful right after a built-in function that signals failure through a warning (such as fopen(), file_get_contents(), or unlink()), and inside a custom shutdown function to catch a fatal error that ended the request.
Examples
Reading the last warning from a failed fopen()
Functions like fopen() return false and raise an E_WARNING when they fail. The @ operator suppresses the warning from being printed, while still letting error_get_last() read it:
<?php
error_reporting(E_ALL);
$handle = @fopen("/no/such/file.txt", "r");
if ($handle === false) {
$error = error_get_last();
echo "Type: " . $error['type'] . "\n";
echo "Message: " . $error['message'] . "\n";
echo "Line: " . $error['line'] . "\n";
}
?>Output:
Type: 2
Message: fopen(/no/such/file.txt): Failed to open stream: No such file or directory
Line: 4The type is 2, which is the value of the E_WARNING constant, and the message comes straight from fopen().
Capturing an error you raise yourself
You can record your own error with trigger_error() and then read it back:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '0');
trigger_error("Invalid configuration value", E_USER_WARNING);
$error = error_get_last();
echo "Message: " . $error['message'] . "\n";
echo "Type: " . $error['type'] . "\n"; // 512 = E_USER_WARNING
?>Output:
Message: Invalid configuration value
Type: 512When no error has occurred
If nothing has gone wrong yet, the function returns null, so always check before reading array keys:
<?php
$error = error_get_last();
var_dump($error); // NULL — guard against this before using $error['message']
?>Output:
NULLCommon gotchas
- Always null-check the result (
if ($error !== null)) before accessing$error['message'], or you will trigger a new "trying to access array offset on null" warning. - Division by zero is not a warning anymore. Use
try/catchforDivisionByZeroError;error_get_last()will not report it. - For programmatic logging rather than inspection, consider pairing this with
error_log()or a custom handler registered throughset_error_handler().
Conclusion
error_get_last() gives you the type, message, file, and line of the most recent error PHP recorded, or null if there was none. Reach for it right after a built-in that fails with a warning, or inside a shutdown function — and remember that exceptions (including modern division-by-zero) need try/catch instead.