删除 PHP 中的警告消息
我有一些 PHP 代码。当我运行它时,会出现一条警告消息。
如何删除/抑制/忽略这些警告消息?
I have some PHP code. When I run it, a warning message appears.
How can I remove/suppress/ignore these warning messages?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
您确实应该修复导致警告的任何原因,但您可以使用 控制错误的可见性
error_reporting()
。要跳过警告消息,您可以使用以下命令:You really should fix whatever's causing the warning, but you can control visibility of errors with
error_reporting()
. To skip warning messages, you could use something like:您可以在函数调用前面放置 @ 来抑制所有错误消息。
You can put an @ in front of your function call to suppress all error messages.
要抑制警告,同时启用所有其他错误报告:
To suppress warnings while leaving all other error reporting enabled:
如果您不想显示警告和错误,请使用
错误报告 - PHP 手册
If you don't want to show warnings as well as errors use
Error Reporting - PHP Manual
如果您想在显示所有其他错误时抑制警告和一些其他错误类型(例如通知),您可以执行以下操作:
If you want to suppress the warnings and some other error types (for example, notices) while displaying all other errors, you can do:
在 Core Php 中隐藏警告消息,将 error_reporting(0) 设置在公共包含文件或单个文件的顶部。
在 Wordpress 中隐藏警告和通知在 wp-config.php 文件中添加以下代码
in Core Php to hide warning message set error_reporting(0) at top of common include file or individual file.
In Wordpress hide Warnings and Notices add following code in wp-config.php file
我在 php.ini 中执行如下操作:
这仅记录致命错误,不记录警告。
I do it as follows in my php.ini:
This logs only fatal errors and no warnings.
不完全回答这个问题,但我认为在某些情况下这是一个更好的妥协:
由于第三方库中的 printf() 语句,我收到了一条警告消息。我确切地知道原因是什么 - 第三方修复其代码时的临时解决方法。我同意不应抑制警告,但我无法向客户展示我的工作,并在屏幕上弹出警告消息。我的解决方案:
警告仍然在页面源代码中作为对我的提醒,但对客户端来说是不可见的。
Not exactly answering the question, but I think this is a better compromise in some situations:
I had a warning message as a result of a printf() statement in a third-party library. I knew exactly what the cause was - a temporary work-around while the third-party fixed their code. I agree that warnings should not be suppressed, but I could not demonstrate my work to a client with the warning message popping up on screen. My solution:
Warning was still in page source as a reminder to me, but invisible to the client.
您可以使用 error_reporting 抑制警告但更好的方法是首先修复你的脚本。
You could suppress the warning using error_reporting but the much better way is to fix your script in the first place.
我认为更好的解决方案是配置 .htaccess 这样您就不必更改应用程序的代码。以下是 Apache2 的指令
I think that better solution is configuration of .htaccess In that way you dont have to alter code of application. Here are directives for Apache2
已经有 错误控制运算符 的答案,但缺少解释。您可以对每个表达式使用
@
运算符,它会隐藏错误(致命错误除外)。对于调试来说,这是快速而完美的方法。但您永远不应该在生产中使用它,也不应该永久包含在您的本地版本中。会给你带来很多不必要的刺激。
您应该考虑:
1.接受的答案中提到的错误报告设置。
或从 PHP INI 设置
2. 捕获异常
There is already answer with Error Control Operator but it lacks of explanation. You can use
@
operator with every expression and it hides errors (except of Fatal Errors).For debugging it's fast and perfect method. But you should never ever use it on production nor permanent include in your local version. It will give you a lot of unnecessary irritation.
You should consider instead:
1. Error reporting settings as mentioned in accepted answer.
or from PHP INI settings
2. Catching exceptions