关闭 PHP 和 MySQL 上的警告和错误

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

Turn off warnings and errors on PHP and MySQL

phpmysql

提问by Ossi

I am getting expected notices and warnings and would like to turn them off in my PHP file. The error is:

我收到了预期的通知和警告,并想在我的 PHP 文件中关闭它们。错误是:

Warning: fsockopen()

And the notice are:

通知是:

Notice: A non well formed numeric value encountered in

I am planning to use cron for this PHP script and do not want to get any errors or notices logged anywhere.

我打算在这个 PHP 脚本中使用 cron,并且不想在任何地方记录任何错误或通知。

回答by pixeline

When you are sure your script is perfectly working, you can get rid of warning and notices like this: Put this line at the beginning of your PHP script:

当您确定您的脚本完美运行时,您可以摆脱这样的警告和通知: 将此行放在您的 PHP 脚本的开头:

error_reporting(E_ERROR);

Before that, when working on your script, I would advise you to properly debug your script so that all notice or warning disappear one by one.

在此之前,在处理您的脚本时,我会建议您正确调试您的脚本,以便所有通知或警告一一消失。

So you should first set it as verbose as possible with:

因此,您应该首先将其设置为尽可能详细:

error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

UPDATE: how to log errors instead of displaying them

更新:如何记录错误而不是显示它们

As suggested in the comments, the better solution is to log errors into a file so only the PHP developer sees the error messages, not the users.

正如评论中所建议的,更好的解决方案是将错误记录到文件中,这样只有 PHP 开发人员才能看到错误消息,而不是用户。

A possible implementation is via the .htaccess file, useful if you don't have access to the php.ini file (source).

一个可能的实现是通过 .htaccess 文件,如果您无权访问 php.ini 文件(source),则很有用。

# Suppress PHP errors
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

# Enable PHP error logging
php_flag  log_errors on
php_value error_log  /home/path/public_html/domain/PHP_errors.log

# Prevent access to PHP error log
<Files PHP_errors.log>
 Order allow,deny
 Deny from all
 Satisfy All
</Files>

回答by Mike B

Prepend functions with the '@' symbol to suppress certain errors, as opposed to turning off allerror reporting.

在函数前面加上“@”符号以抑制某些错误,而不是关闭所有错误报告。

More information: http://php.net/manual/en/language.operators.errorcontrol.php

更多信息:http: //php.net/manual/en/language.operators.errorcontrol.php

PHP supports one error control operator: the at sign (@). When prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored.

PHP 支持一种错误控制运算符:at 符号 (@)。在 PHP 中添加到表达式之前,该表达式可能生成的任何错误消息都将被忽略。

@fsockopen();

回答by Darrell

Always show errors on a testing server. Never show errors on a production server.

始终在测试服务器上显示错误。永远不要在生产服务器上显示错误。

Write a script to determine whether the page is on a local, testing, or live server, and set $state to "local", "testing", or "live". Then:

编写脚本来确定页面是在本地、测试还是实时服务器上,并将 $state 设置为“本地”、“测试”或“实时”。然后:

if( $state == "local" || $state == "testing" )
{
    ini_set( "display_errors", "1" );
    error_reporting( E_ALL & ~E_NOTICE );
}
else
{
    error_reporting( 0 );
}

回答by Shankar Prakash G

PHP error_reporting reference:

PHP error_reporting 参考:

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

// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);

// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

// Report all errors except E_NOTICE
// This is the default value set in php.ini
error_reporting(E_ALL ^ E_NOTICE);

// Report all PHP errors (see changelog)
error_reporting(E_ALL);

// Report all PHP errors
error_reporting(-1);

// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);

回答by Flak DiNenno

If you can't get to your php.ini file for some reason, disable errors to stdout (display_errors) in a .htaccess file in any directory by adding the following line:

如果由于某种原因无法访问 php.ini 文件,请display_errors通过添加以下行来禁用任何目录中 .htaccess 文件中stdout ( ) 的错误:

php_flag display_errors off

php_flag display_errors off

additionally, you can add error logging to a file:

此外,您可以将错误日志添加到文件中:

php_flag log_errors on

php_flag log_errors on

回答by Sabeen Malik

You can set the type of error reporting you need in php.ini or by using the error_reporting() function on top of your script.

您可以在 php.ini 中或使用脚本顶部的 error_reporting() 函数设置您需要的错误报告类型。