Skip to content
Advertisement

How to terminate script on any error? (Like the equivalent bash `set -e` option)

How can I terminate / halt a PHP script and return an error exit code if ANY errors occur?

Basically the functionality that is provided by set -e in Bash?

I’ve tried setting ini_set('error_reporting', E_ALL); but this does not actually halt the script when an error occurs.

Advertisement

Answer

This seems to do the job:

set_error_handler(function ($errno, $errstr, $errfile, $errline) {
    throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
});
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement