search

Home  >  Q&A  >  body text

How does PHP display an error after the script has finished loading?

I have used:

ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

at the beginning of my code. However, the error messages are displayed directly in the output (there are a lot of them, so you can't find the errors very well).

After the script is loaded, how to display all errors that occurred?

P粉585541766P粉585541766234 days ago470

reply all(1)I'll reply

  • P粉521697419

    P粉5216974192024-04-04 00:03:03

    You can use error/exception handlers with destructors. The handler catches the error and the destructor will display the caught error at the very bottom of the page.

    class ErrorHandler {
        private static $instance = null;
        private static $errors = [];
        
        public static function Init() {
            if(!self::$instance) {
                self::$instance = new self;
                
                set_error_handler(function($errno, $errstr, $errfile, $errline){
                    self::$errors[] = new ErrorException($errstr, 0, $errno, $errfile, $errline);
                });
                
                set_exception_handler(function($ex){
                    self::$errors[] = $ex;
                });
            }
        }
        
        function __destruct(){
            foreach(self::$errors as $errstr)
                echo $errstr, '
    '; } } ErrorHandler::Init();

    reply
    0
  • Cancelreply