gpt4 book ai didi

php - 是否可以仅对部分脚本禁用错误报告?

转载 作者:行者123 更新时间:2023-12-03 07:39:49 24 4
gpt4 key购买 nike

PHP 文档 reads (强调我的):

The error_reporting() function sets the error_reporting directive at runtime. PHP has many levels of errors, using this function sets that level for the duration (runtime) of your script. If the optional level is not set, error_reporting() will just return the current error reporting level.

这是否意味着错误报告的级别只能在 PHP 脚本中设置一次,并且之后不能更改?例如,是否可以这样做:

error_reporting(0);

try {
errorSilently();
} catch (Exception $e) {
// Do nothing
}

error_reporting(E_ALL);

try {
errorLOUDLY();
} catch (Exception $e) {
// Do nothing
}

请注意,我写了一个 //Do nothing 因为似乎如果抛出错误,Apache 会以任何方式将其写入 error_log,无论是否被捕获.我想要做的是禁用该行为,而不是写入错误日志。

最佳答案

是的,您可以在程序运行期间随时更改错误报告级别,它将保持在新的级别,直到您再次更改它。您可以仅“静音”特定部分。请注意,error_reporting() 会返回 级别,具体而言,您可以执行以下操作:

$previous = error_reporting(0);

something_which_produces_warnings_which_can_safely_be_ignored();

error_reporting($previous);

要同时暂时禁用错误日志记录,您可能必须ini_set('log_errors', false)

不,try..catch错误报告 无关。这些是独立的机制。 error_reporting 影响 trigger_error 触发的错误; try..catch 是一种处理通过 throw 抛出的异常的机制。

关于php - 是否可以仅对部分脚本禁用错误报告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40279746/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com