Open In App

PHP error_get_last() Function

Last Updated : 29 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The error_get_last() function is an inbuilt PHP function in PHP which is used to get the last error that occurred.

Syntax:

error_get_last(): ?array

Parameter: This function does not accept any parameters.

Return Value: It returns an associate array that explains the last error with keys “type”, “message”, “file” and “line”, which will be returned. The “message” will begin with its name, if the error occurred by PHP internal function, otherwise, return null for not finding any error yet.

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

PHP




<?php
  
$file = 'filedoesnotexist.txt';
$handle = fopen($file, 'r');
    
if ($handle === false) {
    $error = error_get_last();
    echo "Error opening file: " . $error['message'];
else {
      
    // Do something with the file handle
    fclose($handle);
}
?>


Output:

Error opening file: fopen(filedoesnotexist.txt): Failed to open stream: 
No such file or directory

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

PHP




<?php
  
$a = 10 ;
if(error_get_last()) {
    echo "This never will print because no error " ;
}
else {
    echo "No error occurs, so it will return null";
}
?>


Output:

No error occurs, so it will return null  

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads