仅对部分代码禁用 PHP 警告
Disable PHP Warnings for only PART OF the code
为了禁用PHP警告,您需要在php.ini
中设置以下参数
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
display_errors = On
或
display_errors = Off
上述方法将为整个项目禁用警告。有没有办法 仅对 php 代码块 禁用警告,即禁用某些功能或几行代码的警告?
Yes、但是...
理想情况下,您希望修复这些错误(或至少以正确的方式处理它们),因为它看起来像是糟糕的设计,但您正在寻找的是错误控制运算符(@
).
<?php
/* Intentional file error */
$my_file = @file('non_existent_file') or die("Failed opening file: error was '$php_errormsg'");
// this works for any expression, not just functions:
$value = @$cache[$key];
// will not issue a notice if the index $key doesn't exist.
?>
Note: The @-operator works only on expressions. A simple rule of thumb is: if you can take the value of something, you can prepend the @ operator to it. For instance, you can prepend it to variables, function and include calls, constants, and so forth. You cannot prepend it to function or class definitions, or conditional structures such as if and foreach, and so forth.
注意...
Warning: Currently the "@" error-control operator prefix will even disable error reporting for critical errors that will terminate script execution. Among other things, this means that if you use "@" to suppress errors from a certain function and either it isn't available or has been mistyped, the script will die right there with no indication as to why.
首先 - 抑制 E_DEPRECATED
和 E_STRICT
似乎不是一个好的解决方案 - 下一个 PHP 升级会破坏你的代码。应用这应该只是暂时的修复,直到你修复错误。
您有多个选项可以抑制 errors/warnings。
使用@
-operator抑制单个语句的错误。可以这样使用:$content = @file_get_contents('some-url-which-is-maybe-not-available..');
抑制错误,直到您想用 error_reporting
恢复它。这样做就像设置其他 ini 值一样。
一个例子是
$oldlevel = error_reporting(E_ALL & ~E_DEPRECATED & ~E_STRICT);
// some code ...
// revert it
error_reporting($oldlevel);
你应该记住,你不能删除文件的几行编译time/parser errors/warnings。这只能应用于整个文件,如果您在包含它之前更改错误级别。
为了禁用PHP警告,您需要在php.ini
中设置以下参数error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
display_errors = On
或
display_errors = Off
上述方法将为整个项目禁用警告。有没有办法 仅对 php 代码块 禁用警告,即禁用某些功能或几行代码的警告?
Yes、但是...
理想情况下,您希望修复这些错误(或至少以正确的方式处理它们),因为它看起来像是糟糕的设计,但您正在寻找的是错误控制运算符(@
).
<?php
/* Intentional file error */
$my_file = @file('non_existent_file') or die("Failed opening file: error was '$php_errormsg'");
// this works for any expression, not just functions:
$value = @$cache[$key];
// will not issue a notice if the index $key doesn't exist.
?>
Note: The @-operator works only on expressions. A simple rule of thumb is: if you can take the value of something, you can prepend the @ operator to it. For instance, you can prepend it to variables, function and include calls, constants, and so forth. You cannot prepend it to function or class definitions, or conditional structures such as if and foreach, and so forth.
注意...
Warning: Currently the "@" error-control operator prefix will even disable error reporting for critical errors that will terminate script execution. Among other things, this means that if you use "@" to suppress errors from a certain function and either it isn't available or has been mistyped, the script will die right there with no indication as to why.
首先 - 抑制 E_DEPRECATED
和 E_STRICT
似乎不是一个好的解决方案 - 下一个 PHP 升级会破坏你的代码。应用这应该只是暂时的修复,直到你修复错误。
您有多个选项可以抑制 errors/warnings。
使用
@
-operator抑制单个语句的错误。可以这样使用:$content = @file_get_contents('some-url-which-is-maybe-not-available..');
抑制错误,直到您想用
error_reporting
恢复它。这样做就像设置其他 ini 值一样。
一个例子是
$oldlevel = error_reporting(E_ALL & ~E_DEPRECATED & ~E_STRICT);
// some code ...
// revert it
error_reporting($oldlevel);
你应该记住,你不能删除文件的几行编译time/parser errors/warnings。这只能应用于整个文件,如果您在包含它之前更改错误级别。