PHP - 在一个字符串中捕获所有 error_get_last()
PHP - Capture all error_get_last() in One String
是否可以 capture/gather 页面上的所有错误并使用以下方法将它们连接成一个字符串:
$allErrors = "";
$allErrors .= error_get_last(); // Each time an error shows up
我喜欢在我的数据库中记录错误,并且更愿意记录所有这些 PHP 错误,因为我已经记录了 SQL 相关的 PHP 致命错误。
error_get_last(), like the name is suggesting, only gives you the last error. And the fact that most errors will stop your script from running will get you only the last one and none of the previous ones. But you can set an own handler 捕获每个抛出的错误和异常。这是一个例子
//function for exception handling
function handle_exception (Exception $exception) {
//here you can save the exception to your database
}
//function for error handling
function handle_error ($number, $message, $file, $line, $context = null) {
//pass/throw error to handle_exception
throw new ErrorException ($message, 0, $number, $file, $line);
}
//set error-handler but only for E_USER_ERROR and E_RECOVERABLE_ERROR
set_error_handler ('handle_error', E_USER_ERROR|E_RECOVERABLE_ERROR);
//exception-handler
set_exception_handler ('handle_exception');
是否可以 capture/gather 页面上的所有错误并使用以下方法将它们连接成一个字符串:
$allErrors = "";
$allErrors .= error_get_last(); // Each time an error shows up
我喜欢在我的数据库中记录错误,并且更愿意记录所有这些 PHP 错误,因为我已经记录了 SQL 相关的 PHP 致命错误。
error_get_last(), like the name is suggesting, only gives you the last error. And the fact that most errors will stop your script from running will get you only the last one and none of the previous ones. But you can set an own handler 捕获每个抛出的错误和异常。这是一个例子
//function for exception handling
function handle_exception (Exception $exception) {
//here you can save the exception to your database
}
//function for error handling
function handle_error ($number, $message, $file, $line, $context = null) {
//pass/throw error to handle_exception
throw new ErrorException ($message, 0, $number, $file, $line);
}
//set error-handler but only for E_USER_ERROR and E_RECOVERABLE_ERROR
set_error_handler ('handle_error', E_USER_ERROR|E_RECOVERABLE_ERROR);
//exception-handler
set_exception_handler ('handle_exception');