How does PHP display an error after the script has finished loading?
P粉585541766
P粉585541766 2024-04-03 22:11:06
0
1
373

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粉585541766
P粉585541766

reply all(1)
P粉521697419

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();
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!