停止在 PHP 中显示的通知

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/930154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:22:16  来源:igfitidea点击:

Stop Notices From Displaying In PHP

php

提问by Ben Shelock

I want my notices to stop displaying in PHP. Theres no errors in the code, it just says things like undefined index. Which nothing can be done about.

我希望我的通知停止在 PHP 中显示。代码中没有错误,它只是说诸如未定义索引之类的东西。对此无能为力。

So how do I stop it from displaying?

那么如何阻止它显示呢?

Notice: Undefined variable: username in C:\wamp\www\watchedit\includes\config.php on line 37

Notice: Undefined variable: key in C:\wamp\www\watchedit\includes\config.php on line 42

回答by sj2009

This will turn off notices for the environment programmatically-- from PHP.net.

这将以编程方式关闭环境通知——来自 PHP.net。

// Report all errors except E_NOTICE   
error_reporting(E_ALL ^ E_NOTICE);  

In some places, you can prefix the statement with "@" and it will silence just that location if it causes a notice.

在某些地方,您可以在语句前加上“@”,如果它引起通知,它只会使该位置静音。

回答by Gumbo

You should check with issetif the variable exists before trying to read its value.

isset在尝试读取其值之前,您应该检查该变量是否存在。

回答by Jan Jungnickel

Which nothing can be done about.

对此无能为力。

This is not true in most cases. Undefined variables can be declared, undefined indices can be tested for using isset(mixed...).

在大多数情况下,情况并非如此。可以声明未定义的变量,可以使用isset(mixed...)测试未定义的索引。

Also, you should configure your environment as suggested above using error_reporting(...). In production environments it is also recommended to disable display_errors

此外,您应该按照上面的建议使用 error_reporting(...) 配置您的环境。在生产环境中也建议禁用display_errors

回答by user1686

回答by James Hall

Striving to generate no notices is a healthy goal, since they will then begin to flag up potential errors or problems. You can write your own error handler to log these, since they should (hopefully) be few and far between.

努力不产生通知是一个健康的目标,因为他们会开始标记潜在的错误或问题。您可以编写自己的错误处理程序来记录这些,因为它们应该(希望)很少而且相距甚远。

回答by user214393

Because i have no php.ini so i just put this tag right after

因为我没有 php.ini 所以我只是把这个标签放在后面

error_reporting(E_ALL ^ E_NOTICE);