简体   繁体   English

如何禁止在PHP中记录某些E_WARNINGs?

[英]How to suppress logging of certain E_WARNINGs in PHP?

In my script I need to delete a file, that may or may not be there: 在我的脚本中,我需要删除一个文件,该文件可能存在或可能不存在:

unlink($path);

Like the actual unlink(2), PHP's unlink() will unlink the entry, if it is there . 像实际的unlink(2)一样, PHP的unlink()会取消链接条目( 如果存在) If it is not, however, PHP will log the useless (to my purposes) message at E_WARNING level... I guess, this is nice for some people, but not for me :( 但是,如果不是这样,PHP将在E_WARNING级别记录无用的(出于我的目的)消息...我想,这对某些人来说是好的,但对我而言不是:(

Programming in C, I could examine the errno and simply ignore the ENOENT in this case. 在C语言中进行编程,我可以检查errno并在这种情况下直接忽略ENOENT。 What can one do in PHP — how to suppress the logging of this warning? 在PHP中可以做什么?如何禁止记录此警告?

I'd rather not check for the file before trying to unlink it — doing so adds another filesystem-traversal for no reason other than cosmetics: 我宁愿在尝试取消链接之前不检查文件-这样做只会增加其他文件系统遍历,除了修饰外,没有其他原因:

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

Is there a better way? 有没有更好的办法?

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

php.ini configuratioin php.ini配置

I agree with everyone who mentioned using "@" to suppress the error. 我同意提到使用“ @”抑制错误的每个人。

You can also change some of the settings in you php.ini file so that the error won't show up. 您也可以更改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 

This final line error_reporting allows you to change exactly what errors you want to show up. 最后一行error_reporting允许您确切地更改要显示的错误。 In your case, the E_WARNING error is what you are trying to avoid so I would use E_ALL & ~E_WARNING . 在您的情况下, E_WARNING错误是您要避免的错误,因此我将使用E_ALL & ~E_WARNING

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM