如何禁止在PHP中记录某些E_警告?

如何禁止在PHP中记录某些E_警告?,php,unix,Php,Unix,在我的脚本中,我需要删除一个文件,该文件可能存在,也可能不存在: unlink($path); 与实际取消链接(2)一样,如果条目存在,将取消链接。但是,如果不是,PHP将以E_警告级别记录无用(出于我的目的)消息。。。我想,这对一些人来说是好事,但对我来说不是:( 在C语言中编程,我可以检查errno并在本例中忽略enoint 我不希望在尝试取消链接之前检查该文件-这样做会添加另一个文件系统遍历,除了化妆品以外没有其他原因: if (file_exists($path)) unlin

在我的脚本中,我需要删除一个文件,该文件可能存在,也可能不存在:

unlink($path);
与实际取消链接(2)一样,如果条目存在,将取消链接。但是,如果不是,PHP将以E_警告级别记录无用(出于我的目的)消息。。。我想,这对一些人来说是好事,但对我来说不是:(

在C语言中编程,我可以检查errno并在本例中忽略enoint

我不希望在尝试取消链接之前检查该文件-这样做会添加另一个文件系统遍历,除了化妆品以外没有其他原因:

if (file_exists($path))
    unlink($path);

有更好的方法吗?

php.ini配置

我同意大家提到使用“@”来抑制错误的说法

您还可以更改php.ini文件中的一些设置,以避免出现错误

 ; Error Level Constants:
; E_ALL             - All errors and warnings (includes E_STRICT as of PHP 6.0.0)
; E_ERROR           - fatal run-time errors
; E_RECOVERABLE_ERROR  - almost fatal run-time errors
; E_WARNING         - run-time warnings (non-fatal errors)
; E_PARSE           - compile-time parse errors
; E_NOTICE          - run-time notices (these are warnings which often result
;                     from a bug in your code, but it's possible that it was
;                     intentional (e.g., using an uninitialized variable and
;                     relying on the fact it's automatically initialized to an
;                     empty string)
; E_STRICT          - run-time notices, enable to have PHP suggest changes
;                     to your code which will ensure the best interoperability
;                     and forward compatibility of your code
; E_CORE_ERROR      - fatal errors that occur during PHP's initial startup
; E_CORE_WARNING    - warnings (non-fatal errors) that occur during PHP's
;                     initial startup
; E_COMPILE_ERROR   - fatal compile-time errors
; E_COMPILE_WARNING - compile-time warnings (non-fatal errors)
; E_USER_ERROR      - user-generated error message
; E_USER_WARNING    - user-generated warning message
; E_USER_NOTICE     - user-generated notice message
; E_DEPRECATED      - warn about code that will not work in future versions
;                     of PHP
; E_USER_DEPRECATED - user-generated deprecation warnings
;
; Common Values:
;   E_ALL & ~E_NOTICE  (Show all errors, except for notices and coding standards warnings.)
;   E_ALL & ~E_NOTICE | E_STRICT  (Show all errors, except for notices)
;   E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR  (Show only errors)
;   E_ALL | E_STRICT  (Show all errors, warnings and notices including coding standards.)
; Default Value: E_ALL & ~E_NOTICE
; Development Value: E_ALL | E_STRICT
; Production Value: E_ALL & ~E_DEPRECATED
; http://php.net/error-reporting
error_reporting = E_ALL 
此最后一行错误报告允许您准确更改要显示的错误。在您的情况下,
E\u警告
错误是您试图避免的,因此我将使用
E\u ALL&~E\u警告


我希望这会有所帮助。

php.ini configuratioin

我同意大家提到使用“@”来抑制错误的说法

您还可以更改php.ini文件中的一些设置,以避免出现错误

 ; Error Level Constants:
; E_ALL             - All errors and warnings (includes E_STRICT as of PHP 6.0.0)
; E_ERROR           - fatal run-time errors
; E_RECOVERABLE_ERROR  - almost fatal run-time errors
; E_WARNING         - run-time warnings (non-fatal errors)
; E_PARSE           - compile-time parse errors
; E_NOTICE          - run-time notices (these are warnings which often result
;                     from a bug in your code, but it's possible that it was
;                     intentional (e.g., using an uninitialized variable and
;                     relying on the fact it's automatically initialized to an
;                     empty string)
; E_STRICT          - run-time notices, enable to have PHP suggest changes
;                     to your code which will ensure the best interoperability
;                     and forward compatibility of your code
; E_CORE_ERROR      - fatal errors that occur during PHP's initial startup
; E_CORE_WARNING    - warnings (non-fatal errors) that occur during PHP's
;                     initial startup
; E_COMPILE_ERROR   - fatal compile-time errors
; E_COMPILE_WARNING - compile-time warnings (non-fatal errors)
; E_USER_ERROR      - user-generated error message
; E_USER_WARNING    - user-generated warning message
; E_USER_NOTICE     - user-generated notice message
; E_DEPRECATED      - warn about code that will not work in future versions
;                     of PHP
; E_USER_DEPRECATED - user-generated deprecation warnings
;
; Common Values:
;   E_ALL & ~E_NOTICE  (Show all errors, except for notices and coding standards warnings.)
;   E_ALL & ~E_NOTICE | E_STRICT  (Show all errors, except for notices)
;   E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR  (Show only errors)
;   E_ALL | E_STRICT  (Show all errors, warnings and notices including coding standards.)
; Default Value: E_ALL & ~E_NOTICE
; Development Value: E_ALL | E_STRICT
; Production Value: E_ALL & ~E_DEPRECATED
; http://php.net/error-reporting
error_reporting = E_ALL 
此最后一行错误报告允许您准确更改要显示的错误。在您的情况下,
E\u警告
错误是您试图避免的,因此我将使用
E\u ALL&~E\u警告


我希望这会有所帮助。

您可以在表达式前面加上@以仅抑制该表达式的警告(例如,
@unlink($path);
)。

您可以在表达式前面加上@以仅抑制该表达式的警告(例如,
@unlink($path);
).

您可以在表达式前面加上@,以仅抑制该表达式的警告(例如,@unlink($path))。处理错误比抑制错误更好。但是,PHP的错误报告设置可能很有用:是的,在这种情况下只需使用错误抑制运算符
@
。但要注意养成习惯——这种情况是例外,而不是规则。我同意garlon4和Jon的观点,请使用抑制符“@”但是不要总是使用它,特别是对于关键代码。Garlon4,你能把你的评论转换成答案吗,这样我就可以选择它了?谢谢!你可以在表达式前面加上@来抑制该表达式的警告(例如,@unlink($path))。处理错误比抑制错误更好。但是,PHP的错误报告设置可能很有用:是的,在这种情况下只需使用错误抑制运算符
@
。但要注意养成习惯——这种情况是例外,而不是规则。我同意garlon4和Jon的观点,请使用抑制符“@”但是不要总是使用它,特别是对于关键代码。Garlon4,你能把你的评论变成答案吗,这样我就可以选择它了?谢谢!