PHP收到警告和错误消息?

lis*_*aro 7 php error-handling

我想在php $变量中收到警告和错误消息,所以我将它们保存到我的数据库中.

例如,当出现任何类型的错误,警告或类似情况时:

Parse error: syntax error, unexpected T_VARIABLE in /example.php(136) on line 9
Warning: [...]
Run Code Online (Sandbox Code Playgroud)

我想让他们变量$ error_code

这是怎么做到的?

Ja͢*_*͢ck 8

对于只记录它们的简单情况:

set_error_handler(function($errno, $errstr, $errfile, $errline) use ($db) {
    // log in database using $db->query()
});
Run Code Online (Sandbox Code Playgroud)

您可以让这些警告,通知等生成异常,而不是仅将它们记录到您的数据库中(可能您不会在一段时间后查看它们).

function exception_error_handler($errno, $errstr, $errfile, $errline)
{
    if (error_reporting()) { // skip errors that were muffled
        throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
    }
}

set_error_handler("exception_error_handler");
Run Code Online (Sandbox Code Playgroud)

资源: ErrorException

异常将产生更严重的副作用,因此您应该有一个异常处理程序来防止未捕获的异常导致白页死亡.