本文介绍了用PHP中的@运算符抑制错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您认为,使用@运算符来抑制PHP中的错误/警告是否有效,而您可能正在处理该错误?

In your opinion, is it ever valid to use the @ operator to suppress an error/warning in PHP whereas you may be handling the error?

如果是,您将在什么情况下使用它?

If so, in what circumstances would you use this?

欢迎使用代码示例.

答复者注意.我不想关闭错误报告功能,但是,例如,常见的做法是使用

Note to repliers. I'm not looking to turn error reporting off, but, for example, common practice is to use

@fopen($file);

,然后再检查...,但是您可以这样做来摆脱@

and then check afterwards... but you can get rid of the @ by doing

if (file_exists($file))
{
    fopen($file);
}
else
{
    die('File not found');
}

或类似.

我想问题是-@HAS是否有任何地方可以用来抑制错误,并且不能以其他任何方式处理?

I guess the question is - is there anywhere that @ HAS to be used to supress an error, that CANNOT be handled in any other manner?

推荐答案

我将抑制该错误并对其进行处理.否则,您可能会遇到 TOCTOU 问题(检查时间,使用时间.例如,在file_exists返回true之后但在fopen之前,文件可能会被删除).

I would suppress the error and handle it. Otherwise you may have a TOCTOU issue (Time-of-check, time-of-use. For example a file may get deleted after file_exists returns true, but before fopen).

但是我不会仅仅抑制错误以使它们消失.这些更好地可见.

But I wouldn't just suppress errors to make them go away. These better be visible.

这篇关于用PHP中的@运算符抑制错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 04:33