搜索

首页  >  问答  >  正文

PHP如何在脚本加载完成后显示错误?

我已经使用:

1

2

3

ini_set('display_errors', '1');

ini_set('display_startup_errors', '1');

error_reporting(E_ALL);

在我的代码的开头。但是,错误消息直接显示在输出中(数量很多,因此您无法很好地找到错误)。

脚本加载完成后,如何显示发生的所有错误?

P粉585541766P粉585541766338 天前589

全部回复(1)我来回复

  • P粉521697419

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

    您可以将错误/异常处理程序与析构函数一起使用。处理程序捕获错误,析构函数将在页面的最底部显示捕获的错误。

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    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, '<br>';

        }

    }

     

    ErrorHandler::Init();

    回复
    0
  • 取消回复