如何关闭PHP通知?

时间:2022-12-04 07:34:52
Notice: Constant DIR_FS_CATALOG already defined

I've already commented out display_errors in php.ini, but is not working.

我已经注释掉了php中的display_errors。伊妮,但没用。

How do I make PHP to not output such things to browsers?

如何让PHP不向浏览器输出这些东西?

UPDATE

更新

I put display_errors = Off there but it's still reporting such notices,

我把display_errors =放在这里但它仍然报告这样的通知,

Is this an issue with PHP 5.3?

这是PHP 5.3的问题吗?

Reporting numerous Call Stack too..

报告大量的调用堆栈。

14 个解决方案

#1


63  

You can set display_errors to 0 or use the error_reporting() function.

您可以将display_errors设置为0或使用error_reporting()函数。

However, notices are annoying (I can partly sympathize) but they serve a purpose. You shouldn't be defining a constant twice, the second time won't work and the constant will remain unchanged!

然而,通知是令人讨厌的(我能部分地同情它),但它们是有目的的。你不应该定义一个常数两次,第二次不起作用,常数将保持不变!

#2


186  

From the PHP documentation (error_reporting):

PHP文档(error_reporting):

<?php
// Turn off all error reporting
error_reporting(0);
?>

Other interesting options for that function:

该功能的其他有趣选项:

<?php

// 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);
// For PHP < 5.3 use: 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);

?>

#3


25  

For the command line php, set

对于命令行php,设置

error_reporting = E_ALL & ~E_NOTICE

in /etc/php5/cli/php.ini

在/etc/php5/cli/php.ini

command php execution then ommits the notices.

命令php执行然后删除通知。

#4


20  

<?php

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

?>

source http://php.net/manual/en/function.error-reporting.php

源http://php.net/manual/en/function.error-reporting.php

#5


5  

Used This Line In Your Code

在代码中使用了这一行

error_reporting(E_ALL ^ E_NOTICE);  

I think its helf full to you.

我想这对你来说太过分了。

#6


4  

You can set ini_set('display_errors',0); in your script or define which errors you do want to display with error_reporting().

你可以设置报错(display_errors,0);在您的脚本中,或定义您想要显示的错误,请使用error_reporting()。

#7


3  

by not causing the errors:

不产生错误:

defined('DIR_FS_CATALOG') || define('DIR_FS_CATALOG', 'whatever');

If you really have to, then change error reporting using error_reporting() to E_ALL^E_NOTICE.

如果你真的需要,那么改变错误报告使用error_reporting()为E_ALL ^ E_NOTICE。

#8


3  

You are looking for:

你正在寻找:

php -d error_reporting="E_ERROR | E_WARNING | E_PARSE"

#9


2  

For PHP code:

PHP代码:

<?php
error_reporting(E_ALL & ~E_NOTICE);

For php.ini config:

php。ini配置:

error_reporting = E_ALL & ~E_NOTICE

#10


0  

You can check if the constant's already defined using:

您可以使用以下方法检查常数是否已经定义:

<?php
if (!defined('MYCONST'))
    define('MYCONST', 'Weeha!');
?>

#11


0  

I believe commenting out display_errors in php.ini won't work because the default is On. You must set it to 'Off' instead.

我认为应该注释掉php中的display_errors。ini不能工作,因为默认是打开的。你必须把它设置为“Off”。

Don't forget to restart Apache to apply configuration changes.

不要忘记重新启动Apache来应用配置更改。

Also note that while you can set display_errors at runtime, changing it here does not affect FATAL errors.

还要注意,虽然可以在运行时设置display_errors,但是在这里修改它并不会影响致命错误。

As noted by others, ideally during development you should run with error_reporting at the highest level possible and display_errors enabled. While annoying when you first start out, these errors, warnings, notices and strict coding advice all add up and enable you to becoem a better coder.

正如其他人所指出的,理想情况下,在开发过程中,应该在尽可能高的级别上运行error_reporting,并启用display_errors。当你刚开始的时候很烦人,这些错误、警告、通知和严格的编码建议都能让你成为更好的程序员。

#12


0  

I found this trick out recently. Whack an @ at the start of a line that may produce an warning/error.

我最近发现了这个技巧。在可能产生警告/错误的行开始处打一个@。

As if by magic, they dissapear.

就好像是魔法,他们消失了。

#13


0  

Use phpinfo() and search for Configuration File (php.ini) Path to see which config file path for php is used. PHP can have multiple config files depending on environment it's running. Usually, for console it's:

使用phpinfo()和搜索配置文件(php.ini)路径,查看php使用的配置文件路径。PHP可以根据运行的环境拥有多个配置文件。通常,对于控制台:

/etc/php5/cli/php.ini

/etc/php5/cli/php.ini

and for php run by apache it's:

apache运行的php是:

/etc/php5/apache2/php.ini

/etc/php5/apache2/php.ini

And then set error_reporting the way you need it:

然后按需要设置error_reporting:

http://www.phpknowhow.com/configuration/php-ini-error-settings/ http://www.zootemplate.com/news-updates/how-to-disable-notice-and-warning-in-phpini-file

http://www.phpknowhow.com/configuration/php-ini-error-settings/ http://www.zootemplate.com/news-updates/how-to-disable-notice-and-warning-in-phpini-file

#14


0  

As mentioned by some and if you are the code author, you should correct all those errors, notices, etc. because it will cause more problems for you long terms than not fixing them (especially when you upgrade your OS). For your server, you should have errors displayed in your logs only, not the client's screen.

正如一些人所提到的,如果您是代码作者,您应该纠正所有这些错误、通知等,因为它将给您带来更多的问题,而不是修复它们(特别是当您升级您的操作系统时)。对于服务器,应该只在日志中显示错误,而不是在客户端屏幕中显示错误。

So to avoid the errors in your browser you use the display_errors flag as you already found:

因此,为了避免浏览器中的错误,您使用了display_errors标志,正如您已经找到的:

display_errors = Off

Now the real problem is when you are running someone else code. In that case, modifying the code is likely to get overwritten each time you upgrade that code. It makes it tedious to maintain that code.

现在真正的问题是当你运行别人的代码时。在这种情况下,修改代码可能会在每次升级代码时被覆盖。维护这段代码很繁琐。

In my case, I am running PHP with crontab to have the wp-cron.php script running once in a while. I was getting errors sent to my emails, which becomes tedious when you get one email every 10 minutes! In that case, though, the Wordpress system has a configuration file includes a WP_DEBUG and they call the error_reporting() function so trying to change the error_reporting variable on the command line won't work. Instead you have to edit the wp-config.php file in the root folder and make sure that the WP_DEBUG is set to false. Otherwise you will get all those warnings and notices all the time.

在我的例子中,我使用crontab运行PHP,以获得wp-cron。php脚本偶尔运行一次。我收到的错误发送到我的电子邮件,当你每10分钟收到一封电子邮件时,这会变得很乏味!不过,在这种情况下,Wordpress系统有一个配置文件,其中包含一个WP_DEBUG,它们调用error_reporting()函数,因此尝试更改命令行上的error_reporting变量将不起作用。相反,您必须编辑wp-config。根文件夹中的php文件,并确保WP_DEBUG设置为false。否则,您将一直得到所有这些警告和通知。

#1


63  

You can set display_errors to 0 or use the error_reporting() function.

您可以将display_errors设置为0或使用error_reporting()函数。

However, notices are annoying (I can partly sympathize) but they serve a purpose. You shouldn't be defining a constant twice, the second time won't work and the constant will remain unchanged!

然而,通知是令人讨厌的(我能部分地同情它),但它们是有目的的。你不应该定义一个常数两次,第二次不起作用,常数将保持不变!

#2


186  

From the PHP documentation (error_reporting):

PHP文档(error_reporting):

<?php
// Turn off all error reporting
error_reporting(0);
?>

Other interesting options for that function:

该功能的其他有趣选项:

<?php

// 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);
// For PHP < 5.3 use: 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);

?>

#3


25  

For the command line php, set

对于命令行php,设置

error_reporting = E_ALL & ~E_NOTICE

in /etc/php5/cli/php.ini

在/etc/php5/cli/php.ini

command php execution then ommits the notices.

命令php执行然后删除通知。

#4


20  

<?php

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

?>

source http://php.net/manual/en/function.error-reporting.php

源http://php.net/manual/en/function.error-reporting.php

#5


5  

Used This Line In Your Code

在代码中使用了这一行

error_reporting(E_ALL ^ E_NOTICE);  

I think its helf full to you.

我想这对你来说太过分了。

#6


4  

You can set ini_set('display_errors',0); in your script or define which errors you do want to display with error_reporting().

你可以设置报错(display_errors,0);在您的脚本中,或定义您想要显示的错误,请使用error_reporting()。

#7


3  

by not causing the errors:

不产生错误:

defined('DIR_FS_CATALOG') || define('DIR_FS_CATALOG', 'whatever');

If you really have to, then change error reporting using error_reporting() to E_ALL^E_NOTICE.

如果你真的需要,那么改变错误报告使用error_reporting()为E_ALL ^ E_NOTICE。

#8


3  

You are looking for:

你正在寻找:

php -d error_reporting="E_ERROR | E_WARNING | E_PARSE"

#9


2  

For PHP code:

PHP代码:

<?php
error_reporting(E_ALL & ~E_NOTICE);

For php.ini config:

php。ini配置:

error_reporting = E_ALL & ~E_NOTICE

#10


0  

You can check if the constant's already defined using:

您可以使用以下方法检查常数是否已经定义:

<?php
if (!defined('MYCONST'))
    define('MYCONST', 'Weeha!');
?>

#11


0  

I believe commenting out display_errors in php.ini won't work because the default is On. You must set it to 'Off' instead.

我认为应该注释掉php中的display_errors。ini不能工作,因为默认是打开的。你必须把它设置为“Off”。

Don't forget to restart Apache to apply configuration changes.

不要忘记重新启动Apache来应用配置更改。

Also note that while you can set display_errors at runtime, changing it here does not affect FATAL errors.

还要注意,虽然可以在运行时设置display_errors,但是在这里修改它并不会影响致命错误。

As noted by others, ideally during development you should run with error_reporting at the highest level possible and display_errors enabled. While annoying when you first start out, these errors, warnings, notices and strict coding advice all add up and enable you to becoem a better coder.

正如其他人所指出的,理想情况下,在开发过程中,应该在尽可能高的级别上运行error_reporting,并启用display_errors。当你刚开始的时候很烦人,这些错误、警告、通知和严格的编码建议都能让你成为更好的程序员。

#12


0  

I found this trick out recently. Whack an @ at the start of a line that may produce an warning/error.

我最近发现了这个技巧。在可能产生警告/错误的行开始处打一个@。

As if by magic, they dissapear.

就好像是魔法,他们消失了。

#13


0  

Use phpinfo() and search for Configuration File (php.ini) Path to see which config file path for php is used. PHP can have multiple config files depending on environment it's running. Usually, for console it's:

使用phpinfo()和搜索配置文件(php.ini)路径,查看php使用的配置文件路径。PHP可以根据运行的环境拥有多个配置文件。通常,对于控制台:

/etc/php5/cli/php.ini

/etc/php5/cli/php.ini

and for php run by apache it's:

apache运行的php是:

/etc/php5/apache2/php.ini

/etc/php5/apache2/php.ini

And then set error_reporting the way you need it:

然后按需要设置error_reporting:

http://www.phpknowhow.com/configuration/php-ini-error-settings/ http://www.zootemplate.com/news-updates/how-to-disable-notice-and-warning-in-phpini-file

http://www.phpknowhow.com/configuration/php-ini-error-settings/ http://www.zootemplate.com/news-updates/how-to-disable-notice-and-warning-in-phpini-file

#14


0  

As mentioned by some and if you are the code author, you should correct all those errors, notices, etc. because it will cause more problems for you long terms than not fixing them (especially when you upgrade your OS). For your server, you should have errors displayed in your logs only, not the client's screen.

正如一些人所提到的,如果您是代码作者,您应该纠正所有这些错误、通知等,因为它将给您带来更多的问题,而不是修复它们(特别是当您升级您的操作系统时)。对于服务器,应该只在日志中显示错误,而不是在客户端屏幕中显示错误。

So to avoid the errors in your browser you use the display_errors flag as you already found:

因此,为了避免浏览器中的错误,您使用了display_errors标志,正如您已经找到的:

display_errors = Off

Now the real problem is when you are running someone else code. In that case, modifying the code is likely to get overwritten each time you upgrade that code. It makes it tedious to maintain that code.

现在真正的问题是当你运行别人的代码时。在这种情况下,修改代码可能会在每次升级代码时被覆盖。维护这段代码很繁琐。

In my case, I am running PHP with crontab to have the wp-cron.php script running once in a while. I was getting errors sent to my emails, which becomes tedious when you get one email every 10 minutes! In that case, though, the Wordpress system has a configuration file includes a WP_DEBUG and they call the error_reporting() function so trying to change the error_reporting variable on the command line won't work. Instead you have to edit the wp-config.php file in the root folder and make sure that the WP_DEBUG is set to false. Otherwise you will get all those warnings and notices all the time.

在我的例子中,我使用crontab运行PHP,以获得wp-cron。php脚本偶尔运行一次。我收到的错误发送到我的电子邮件,当你每10分钟收到一封电子邮件时,这会变得很乏味!不过,在这种情况下,Wordpress系统有一个配置文件,其中包含一个WP_DEBUG,它们调用error_reporting()函数,因此尝试更改命令行上的error_reporting变量将不起作用。相反,您必须编辑wp-config。根文件夹中的php文件,并确保WP_DEBUG设置为false。否则,您将一直得到所有这些警告和通知。