如何关闭 PHP 通知?

发布于 2024-09-02 00:28:30 字数 391 浏览 5 评论 0 原文

Notice: Constant DIR_FS_CATALOG already defined

我已经在 php.ini 中注释掉了 display_errors ,但不起作用。

如何让 PHP 不向浏览器输出此类内容?

更新

我在那里设置了 display_errors = Off 但它仍然报告此类通知,

这是 PHP 5.3 的问题吗?

也报告大量调用堆栈..

Notice: Constant DIR_FS_CATALOG already defined

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

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

UPDATE

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

Is this an issue with PHP 5.3?

Reporting numerous Call Stack too..

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

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

发布评论

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

评论(18

歌入人心 2024-09-09 00:28:30

您可以通过将错误报告级别设置为 E_ALL & 来禁用通知。 ~E_NOTICE; 使用 error_reporting ini 设置或 error_reporting() 函数。

然而,通知很烦人(我可以部分同情),但它们有一个目的。您不应该两次定义一个常量,第二次将不起作用并且该常量将保持不变!

You can disable notices by setting error reporting level to E_ALL & ~E_NOTICE; using either error_reporting ini setting or the error_reporting() function.

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!

诠释孤独 2024-09-09 00:28:30

对于命令行php,

error_reporting = E_ALL & ~E_NOTICE

/etc/php5/cli/php.ini

设置命令php执行然后忽略该通知。

For the command line php, set

error_reporting = E_ALL & ~E_NOTICE

in /etc/php5/cli/php.ini

command php execution then ommits the notices.

我ぃ本無心為│何有愛 2024-09-09 00:28:30

在您的代码中使用了这一行

error_reporting(E_ALL ^ E_NOTICE);  

我认为它对您很有帮助。

Used This Line In Your Code

error_reporting(E_ALL ^ E_NOTICE);  

I think its helf full to you.

握住我的手 2024-09-09 00:28:30

我不想在代码中设置 error_reporting 。但在一种情况下,遗留产品有太多通知,必须将它们隐藏起来。

因此,我使用以下代码片段来设置 error_reporting 的服务器端配置值,但减去 E_NOTICE

error_reporting(error_reporting() & ~E_NOTICE);

现在可以在 php.ini.htaccess 中进一步配置错误报告设置。只有通知将始终被禁用。

I prefer to not set the error_reporting inside my code. But in one case, a legacy product, there are so many notices, that they must be hidden.

So I used following snippet to set the serverside configured value for error_reporting but subtract the E_NOTICEs.

error_reporting(error_reporting() & ~E_NOTICE);

Now the error reporting setting can further be configured in php.ini or .htaccess. Only notices will always be disabled.

伊面 2024-09-09 00:28:30

您正在寻找:

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

You are looking for:

php -d error_reporting="E_ERROR | E_WARNING | E_PARSE"
那小子欠揍 2024-09-09 00:28:30

对于 PHP 代码:

<?php
error_reporting(E_ALL & ~E_NOTICE);

对于 php.ini 配置:

error_reporting = E_ALL & ~E_NOTICE

For PHP code:

<?php
error_reporting(E_ALL & ~E_NOTICE);

For php.ini config:

error_reporting = E_ALL & ~E_NOTICE
英雄似剑 2024-09-09 00:28:30

我最近发现了这个技巧。
在行的开头敲击 @ 可能会产生警告/错误。

就像施了魔法一样,它们消失了。

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.

独行侠 2024-09-09 00:28:30

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

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

那请放手 2024-09-09 00:28:30

错误报告(E_ERROR);为我工作。

error_reporting(E_ERROR); worked for me.

初熏 2024-09-09 00:28:30

通过不引起错误:

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

如果确实必须这样做,则将使用 error_reporting() 的错误报告更改为 E_ALL^E_NOTICE。

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.

紫南 2024-09-09 00:28:30

如果您从命令行运行,则可以执行以下操作:

php -d display_errors="0" script.php 2>/dev/null

您必须将 -d display_errors="0" 包含为以及 stderr 重定向到 null,很奇怪

If you are running from the command line, you can do this:

php -d display_errors="0" script.php 2>/dev/null

You HAVE to include -d display_errors="0" as well as the redirection of stderr to null, weird

┊风居住的梦幻卍 2024-09-09 00:28:30

使用 phpinfo() 并搜索配置文件(php.ini)路径以查看使用了哪个 php 配置文件路径。 PHP 可以有多个配置文件,具体取决于其运行的环境。通常,对于控制台,它是:

/etc/php5/cli/php.ini

对于由 apache 运行的 php,它是:

/etc/php5/apache2/php.ini

然后根据您需要的方式设置 error_reporting

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

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:

/etc/php5/cli/php.ini

and for php run by apache it's:

/etc/php5/apache2/php.ini

And then set error_reporting the way you need it:

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

乞讨 2024-09-09 00:28:30

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

因此,为了避免浏览器中出现错误,您可以使用您已经发现的 display_errors 标志:

display_errors = Off

现在真正的问题是当您运行其他人的代码时。在这种情况下,每次升级代码时,修改代码可能会被覆盖。维护该代码变得很乏味。

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

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

叫思念不要吵 2024-09-09 00:28:30

双重定义常量

要修复此处的特定错误,您可以在定义常量之前检查常量是否已定义:

if ( ! defined( 'DIR_FS_CATALOG' ) ) 
  define( 'DIR_FS_CATALOG', 'something...' );

我个人会在代码库中搜索常量DIR_FS_CATALOG,然后用此替换双重定义。

根据具体情况隐藏 PHP 内联通知

PHP 提供了 @ 错误控制运算符,您可以使用它来忽略导致通知或警告的特定函数。

使用此功能,您可以在代码中根据具体情况忽略/禁用通知和警告,这对于故意、计划或只是非常烦人且无法从源头解决错误或通知的情况非常有用。在引起通知的函数或 var 之前放置一个 @ ,它将被忽略。

下面是一个示例:

// Intentional file error
$missing_file = @file( 'non_existent_file' );

有关此内容的更多信息,请参阅 PHP 的错误控制运算符< /a> 文档。

Double defined constants

To fix the specific error here you can check if a constant is already defined before defining it:

if ( ! defined( 'DIR_FS_CATALOG' ) ) 
  define( 'DIR_FS_CATALOG', 'something...' );

I'd personally start with a search in the codebase for the constant DIR_FS_CATALOG, then replace the double definition with this.

Hiding PHP notices inline, case-by-case

PHP provides the @ error control operator, which you can use to ignore specific functions that cause notices or warnings.

Using this you can ignore/disable notices and warnings on a case-by-case basis in your code, which can be useful for situations where an error or notice is intentional, planned, or just downright annoying and not possible to solve at the source. Place an @ before the function or var that's causing a notice and it will be ignored.

Here's an example:

// Intentional file error
$missing_file = @file( 'non_existent_file' );

More on this can be found in PHP's Error Control Operators docs.

鹿! 2024-09-09 00:28:30

您可以使用以下命令检查常量是否已定义:

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

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

<?php
if (!defined('MYCONST'))
    define('MYCONST', 'Weeha!');
?>
安静 2024-09-09 00:28:30

我相信在 php.ini 中注释掉 display_errors 是行不通的,因为默认值是 On。您必须将其设置为“关闭”。

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

另请注意,虽然您可以在运行时设置 display_errors,但此处更改它不会影响致命错误。

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

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

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

Also note that while you can set display_errors at runtime, changing it here does not affect FATAL 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.

和我恋爱吧 2024-09-09 00:28:30

设置display_errors=Off并重新启动xampp服务器

Set the display_errors=Off and restart xampp server

允世 2024-09-09 00:28:30

这绝对对我有帮助!

https://www.youtube.com/watch?v=Mfq2xTsOHDg&ab_channel= simplePHPstuff

将此代码放在文件的开头(如果您正在使用它,则在 session_start() 之后)

error_reporting (E_ERROR | E_PARSE);

祝您好运!

This helpend me DEFENITELY!

https://www.youtube.com/watch?v=Mfq2xTsOHDg&ab_channel=simplePHPstuff

Put this code at the beginning of your file (after session_start() if you are using it)

error_reporting (E_ERROR | E_PARSE);

Have luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文