Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save clarkphp/88dff56484146b87628c7ab2b8701dcf to your computer and use it in GitHub Desktop.
Save clarkphp/88dff56484146b87628c7ab2b8701dcf to your computer and use it in GitHub Desktop.
PHP 5 and 7 compatible use of $php_errormsg, , error_clear_last(), and error_get_last()

Before we start

This is really short, but it saved the day, okay?

The Problem

You're migrating from PHP 5 to PHP 7 - about time, isn't it? - and you're replacing $php_errormsg with error_clear_last() and error_get_last(), but you realize the newer functions are PHP 7 only, and you can't deploy the updated code to your thousands of servers all at once, because you live in the real world.

This particular application has to run under both PHP 5 and PHP 7.

The Solution

$php_errormsg = '';
if (function_exists('error_clear_last')) {
    error_clear_last();
}

...

if (function_exists('error_get_last')) {
    if (! is_null(error_get_last())) {
        $php_errormsg = error_get_last()['message'];
    }
}

Now, you can at least use $php_errormsg variable in your code, as before. Under PHP 7, you can make also use of the other indexes in the array returned; namely, [type], [file], and [line].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment