gpt4 book ai didi

php - 如何抑制 codeigniter 中的通知错误?

转载 作者:可可西里 更新时间:2023-11-01 00:09:08 25 4
gpt4 key购买 nike

我想在某个 cronjob 期间抑制 codeigniter 错误日志中的通知。我试过在该行前面使用 @,但它仍然在日志中打印通知:

这是生成通知的行:

@$resultarray[1][2]++;

通知:... 严重性:通知 --> undefined offset :1 ...

我在这里做错了什么?

(我在使用它时考虑周到,所以请不要发消息声称不使用@,谢谢 :) )

最佳答案

要让 PHP 报告除 E_Notice 之外的所有错误,请像这样调用 PHP error_reporting 函数:

error_reporting(E_ALL & ~E_NOTICE);

我认为最好的方法是在应用程序的根目录配置 index.php 以首先检测您的应用程序环境,然后有条件地设置报告哪些 PHP 错误。

索引.php

if (defined('ENVIRONMENT'))
{
switch (ENVIRONMENT)
{
case 'development':
case 'testing':
case 'staging':
error_reporting(E_ALL & ~E_NOTICE);
break;

case 'production':
error_reporting(0);
break;

default:
exit('The application environment is not set correctly.');
}
}

看官方PHP Documentation关于更多配置选项的 error_reporting 函数。

或者,您可以只在您正在使用的特定文件中调用 error_reporting 函数(无论您的 cron 作业调用什么),它将用您在 index.php 中设置的配置覆盖。这是一个使用 Controller 的简单示例:

class Sample_Controller extends CI_Controller {

public function __construct()
{
parent::__construct();
error_reporting(E_ALL & ~E_NOTICE);
}

}

关于php - 如何抑制 codeigniter 中的通知错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23214961/

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