Capturing and Handling PHP Errors Gracefully

Capturing errors are essential in reducing bugs and debugging PHP code. Whether it’s a simple undefined variable or a more complex memory allocation error, it’s important that you are made aware of these errors. Not only that but if a frontend user sees an error, or if something doesn’t work as it should, it can instantly effect their view of your website or application, sometimes even putting them off enough to move on elsewhere. Remember that even though you’ve tested your code thoroughly there will always be a scenario that you haven’t tested. You can be sure that if there’s an error a user one day will find it.

Another scenario I ran into recently was where a script was running silently as a background process. Due to a fatal error somewhere whilst it was running the script stopped suddenly and, due to it running silently I was unaware that anything had gone wrong for a good few hours.

The first function, register_shutdown_function(), allows us to specify a function that will be executed as soon as the PHP script stops running, whether it be from finishing naturally due it coming to the end, or failing because of a fatal PHP error.

The second function, error_get_last(), will pass back the last error, if any, that occured during the running of the script. By putting these two functions together we can create a pretty nifty way of detecting why a script might of failed. Allow me to show you some examples:

Handle All Errors

register_shutdown_function('handleErrors');
function handleErrors() {
$last_error = error_get_last();
if (!is_null($last_error)) { // if there has been an error at some point
// do something with the error
print_r($last_error);
}
}

Should an error occur, the above code would output an array containing information about the last error encountered like so:

The above example might be a bit overkill for a lot of developers due to the fact it will return any type of error, including simple undefined errors. What about if we only want to know about fatal PHP errors that caused the script to stop running completely? Then try this:

I’ve found that by using the above snippets I’ve managed to iron out lots of bugs and errors that I was completely unaware of. You can bet your bottom dollar that a user wouldn’t report an error if they encountered one so this allows me to act on their behalf and deal with any problems that arise.

you could just use error_log. register_shutdown_function looks sooo clever and great at first look, but if you think about iut a while, you will see that there is no real value to it. error_log does log errors good enough and sending errors as emails is stupid. better feed your error_log to a good monitoring system, that can generate tickets etc.