Search code examples
phpshutdown-hook

how to check if exit is clean in a shutdown function in PHP?


How can I test if exit is clean in a shutdown function in PHP?

By clean exit I mean that the script was not terminated due to an error.


Solution

  • Its a good question, for the moment I only have this idea: register a shutdown function like this:

    function shutdown() {
        if (defined('END_REACHED') {
            echo 'Script executed with no error', PHP_EOL;
        }
    }
    
    register_shutdown_function('shutdown');
    

    With auto_append_file add an additional file to the very end of every script execution which sets an constant.

    Your auto_append_file.php content:

    define('END_REACHED', TRUE);
    

    In case you dont want to edit the php.ini you could do check error_get_last() or $php_errormsg in your register shutdown function!