删除 PHP 中的警告消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1987579/
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 04:33:04  来源:igfitidea点击:

Remove warning messages in PHP

phpwarningserror-reporting

提问by Alireza

I have some PHP code. When I run it, a warning message appears.

我有一些 PHP 代码。当我运行它时,会出现一条警告消息。

How can I remove/suppress/ignore these warning messages?

如何删除/抑制/忽略这些警告消息?

回答by Tatu Ulmanen

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:

您确实应该修复导致警告的任何内容,但您可以使用error_reporting(). 要跳过警告消息,您可以使用以下内容:

error_reporting(E_ERROR | E_PARSE);

回答by PetPaulsen

You can put an @in front of your function call to suppress all error messages.

您可以在函数调用前放置一个@以抑制所有错误消息。

@yourFunctionHere();

回答by Karthik

To suppress warnings while leaving all other error reporting enabled:

要在启用所有其他错误报告的同时抑制警告:

error_reporting(E_ALL ^ E_WARNING); 

回答by mohan.gade

If you don't want to show warnings as well as errors use

如果您不想显示警告和错误,请使用

// Turn off all error reporting
error_reporting(0);

Error Reporting - PHP Manual

错误报告 - PHP 手册

回答by zstate

If you want to suppress the warnings and some other error types (for example, notices) while displaying all other errors, you can do:

如果您想在显示所有其他错误的同时抑制警告和其他一些错误类型(例如,通知),您可以执行以下操作:

error_reporting(E_ALL & ~E_WARNING & ~E_NOTICE);

回答by Vijay Lathiya

in Core Php to hide warning message set error_reporting(0) at top of common include file or individual file.

在核心 PHP 中隐藏警告消息设置 error_reporting(0) 在公共包含文件或单个文件的顶部。

In Wordpress hide Warnings and Noticesadd following code in wp-config.php file

在 Wordpress 中隐藏警告和通知在 wp-config.php 文件中添加以下代码

ini_set('log_errors','On');
ini_set('display_errors','Off');
ini_set('error_reporting', E_ALL );
define('WP_DEBUG', false);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);

回答by DaveWalley

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:

由于第三方库中的 printf() 语句,我收到一条警告消息。我确切地知道原因是什么 - 在第三方修复他们的代码时临时解决。我同意不应抑制警告,但我无法在屏幕上弹出警告消息的情况下向客户展示我的工作。我的解决方案:

printf('<div style="display:none">');
    ...Third-party stuff here...
printf('</div>');

Warning was still in page source as a reminder to me, but invisible to the client.

警告仍然在页面源中作为对我的提醒,但对客户端不可见。

回答by navid

I do it as follows in my php.ini:

我在我的php.ini 中按如下方式执行:

error_reporting = E_ALL & ~E_WARNING  & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED

This logs only fatal errors and no warnings.

这仅记录致命错误,不记录警告。

回答by Sebastian Piskorski

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

我认为更好的解决方案是配置 .htaccess 这样您就不必更改应用程序代码。这是 Apache2 的指令

php_flag display_startup_errors off
php_flag display_errors off
php_flag html_errors off
php_value docref_root 0
php_value docref_ext 0

回答by Pekka

You could suppress the warning using error_reportingbut the much better way is to fix your script in the first place.

您可以使用error_reporting抑制警告,但更好的方法是首先修复您的脚本。

If you don't know how, edit your question and show us the line in question and the warning that is displayed.

如果您不知道如何操作,请编辑您的问题并向我们展示有问题的行和显示的警告。