如何关闭 PHP 通知?
Notice: Constant DIR_FS_CATALOG already defined
我已经在 php.ini
中注释掉了 display_errors
,但不起作用。
如何让 PHP 不向浏览器输出此类内容?
更新
我在那里设置了 display_errors = Off
但它仍然报告此类通知,
这是 PHP 5.3 的问题吗?
也报告大量调用堆栈..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
您可以通过将错误报告级别设置为
E_ALL & 来禁用通知。 ~E_NOTICE;
使用error_reporting
ini 设置或error_reporting()
函数。然而,通知很烦人(我可以部分同情),但它们有一个目的。您不应该两次定义一个常量,第二次将不起作用并且该常量将保持不变!
You can disable notices by setting error reporting level to
E_ALL & ~E_NOTICE;
using eithererror_reporting
ini setting or theerror_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!
对于命令行php,
在
/etc/php5/cli/php.ini
中设置命令
php
执行然后忽略该通知。For the command line php, set
in
/etc/php5/cli/php.ini
command
php
execution then ommits the notices.在您的代码中使用了这一行
我认为它对您很有帮助。
Used This Line In Your Code
I think its helf full to you.
我不想在代码中设置
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 theE_NOTICE
s.Now the error reporting setting can further be configured in
php.ini
or.htaccess
. Only notices will always be disabled.您正在寻找:
You are looking for:
对于 PHP 代码:
对于
php.ini
配置:For PHP code:
For
php.ini
config:我最近发现了这个技巧。
在行的开头敲击 @ 可能会产生警告/错误。
就像施了魔法一样,它们消失了。
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.
您可以在脚本中设置
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 witherror_reporting()
.错误报告(E_ERROR);为我工作。
error_reporting(E_ERROR); worked for me.
通过不引起错误:
如果确实必须这样做,则将使用 error_reporting() 的错误报告更改为 E_ALL^E_NOTICE。
by not causing the errors:
If you really have to, then change error reporting using error_reporting() to E_ALL^E_NOTICE.
如果您从命令行运行,则可以执行以下操作:
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
使用 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
正如一些人提到的,如果您是代码作者,您应该纠正所有这些错误、通知等,因为从长远来看,这会给您带来比不修复它们更多的问题(特别是当您升级操作系统时)。对于您的服务器,您应该仅在日志中显示错误,而不是在客户端屏幕中显示错误。
因此,为了避免浏览器中出现错误,您可以使用您已经发现的
display_errors
标志:现在真正的问题是当您运行其他人的代码时。在这种情况下,每次升级代码时,修改代码可能会被覆盖。维护该代码变得很乏味。
就我而言,我使用
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: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 thewp-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 aWP_DEBUG
and they call theerror_reporting()
function so trying to change theerror_reporting
variable on the command line won't work. Instead you have to edit thewp-config.php
file in the root folder and make sure that theWP_DEBUG
is set tofalse
. Otherwise you will get all those warnings and notices all the time.双重定义常量
要修复此处的特定错误,您可以在定义常量之前检查常量是否已定义:
我个人会在代码库中搜索常量
DIR_FS_CATALOG
,然后用此替换双重定义。根据具体情况隐藏 PHP 内联通知
PHP 提供了
@
错误控制运算符,您可以使用它来忽略导致通知或警告的特定函数。使用此功能,您可以在代码中根据具体情况忽略/禁用通知和警告,这对于故意、计划或只是非常烦人且无法从源头解决错误或通知的情况非常有用。在引起通知的函数或 var 之前放置一个
@
,它将被忽略。下面是一个示例:
有关此内容的更多信息,请参阅 PHP 的错误控制运算符< /a> 文档。
Double defined constants
To fix the specific error here you can check if a constant is already defined before defining it:
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:
More on this can be found in PHP's Error Control Operators docs.
您可以使用以下命令检查常量是否已定义:
You can check if the constant's already defined using:
我相信在 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.
设置display_errors=Off并重新启动xampp服务器
Set the display_errors=Off and restart xampp server
这绝对对我有帮助!
https://www.youtube.com/watch?v=Mfq2xTsOHDg&ab_channel= simplePHPstuff
将此代码放在文件的开头(如果您正在使用它,则在 session_start() 之后)
祝您好运!
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)
Have luck!