删除 PHP 中的警告消息

发布于 2024-08-16 17:11:48 字数 62 浏览 7 评论 0原文

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

玩世 2024-08-23 17:11:48

您确实应该修复导致警告的任何原因,但您可以使用 控制错误的可见性error_reporting()。要跳过警告消息,您可以使用以下命令:

error_reporting(E_ERROR | E_PARSE);

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(E_ERROR | E_PARSE);
迷离° 2024-08-23 17:11:48

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

@yourFunctionHere();

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

@yourFunctionHere();
一瞬间的火花 2024-08-23 17:11:48

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

error_reporting(E_ALL ^ E_WARNING);

To suppress warnings while leaving all other error reporting enabled:

error_reporting(E_ALL ^ E_WARNING);
薄荷港 2024-08-23 17:11:48

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

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

错误报告 - PHP 手册

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

梦途 2024-08-23 17:11:48

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

error_reporting(E_ALL & ~E_WARNING & ~E_NOTICE);

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);
窗影残 2024-08-23 17:11:48

在 Core Php 中隐藏警告消息,将 error_reporting(0) 设置在公共包含文件或单个文件的顶部。

在 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);

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

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);
少年亿悲伤 2024-08-23 17:11:48

我在 php.ini 中执行如下操作:

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

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

I do it as follows in my php.ini:

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

This logs only fatal errors and no warnings.

云归处 2024-08-23 17:11:48

不完全回答这个问题,但我认为在某些情况下这是一个更好的妥协:

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

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

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

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('<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.

地狱即天堂 2024-08-23 17:11:48

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

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

A君 2024-08-23 17:11:48

我认为更好的解决方案是配置 .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

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

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
记忆里有你的影子 2024-08-23 17:11:48

已经有 错误控制运算符 的答案,但缺少解释。您可以对每个表达式使用 @ 运算符,它会隐藏错误(致命错误除外)。

@$test['test']; //PHP Notice:  Undefined variable: test

@(14/0); // PHP Warning:  Division by zero

//This is not working. You can't hide Fatal Errors this way.
@customFuntion(); // PHP Fatal error:  Uncaught Error: Call to undefined function customFuntion()

对于调试来说,这是快速而完美的方法。但您永远不应该在生产中使用它,也不应该永久包含在您的本地版本中。会给你带来很多不必要的刺激。

您应该考虑:

1.接受的答案中提到的错误报告设置。

error_reporting(E_ERROR | E_PARSE);

或从 PHP INI 设置

ini_set('display_errors','Off');

2. 捕获异常

try {
    $var->method();
} catch (Error $e) {
    // Handle error
    echo $e->getMessage();
}

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).

@$test['test']; //PHP Notice:  Undefined variable: test

@(14/0); // PHP Warning:  Division by zero

//This is not working. You can't hide Fatal Errors this way.
@customFuntion(); // PHP Fatal error:  Uncaught Error: Call to undefined function customFuntion()

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.

error_reporting(E_ERROR | E_PARSE);

or from PHP INI settings

ini_set('display_errors','Off');

2. Catching exceptions

try {
    $var->method();
} catch (Error $e) {
    // Handle error
    echo $e->getMessage();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文