我应该在PHP代码中使用

我应该在PHP代码中使用

本文介绍了我应该在PHP代码中使用@吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在代码中使用@,会影响性能吗?

If I use @ in my code, will it affect performance?

推荐答案

本文有助于回答您的问题:"> http://anvilstudios.co.za/blog/php/how-to-ignore-errors-in-a-custom- php-error-handler/

This article is helpful for answering your question: http://anvilstudios.co.za/blog/php/how-to-ignore-errors-in-a-custom-php-error-handler/

具体来说,"@有其用途"部分:

Specifically the section "@ has its uses":

  • 您可能正在使用一些使用@的大型外部库,因此需要能够忽略作者的这些错误. 预期的库,否则您的程序将跳闸 不需要的地方.您可以编辑库,但可能会 需要很多时间,您的更改将不得不再次应用 每次作者向库发布更新时.

  • You could be using some large external library which has made use of the @, and so need to be able to ignore those errors as the author of the library intended, otherwise your program is going to trip up where it doesn’t need to. You could edit the library, but it might take a lot of time, and your changes would again have to be applied each time the author releases an update to the library.

另一个示例可能是使用fopen函数打开外部URL,并且该URL无法为多种可能的一种打开 原因.该函数返回false表示失败,即 很好,但引用PHP手册,级别为E_WARNING的错误是 生成的",也不太好-确实应该导致异常 相反,它被抛出,因为这是一种不规则的情况,但是 应该是可以预期的. 在这种情况下,我们希望能够忽略 错误,并继续执行程序,明确地 以适当的方式做出回应-到底是什么例外! 但是,有一种方法可以将错误转换为异常,因此 避免在这种情况下使用@.在您的自定义错误处理程序中 (这是我们在这篇文章中发现自己的地方),然后抛出一个 ErrorException –这然后需要您显式捕获并处理 它在以前使用@的代码中,这是一种更好的方法 处理错误.

Another example might be when the fopen function is used to open an external URL, and the URL cannot be opened for one of many possible reasons. The function returns false to indicate a fail, which is great, but to quote the PHP manual, "an error of level E_WARNING is generated" too, not so great — it should really result in an exception being thrown instead, as this is an irregular situation, but one which should be expected. In this case one would like to be able to ignore the error, and continue with the program execution, explicitly responding in an appropriate way – exactly what exceptions are for! There is, however, a way to convert the error to an exception and so avoid using the @ in this situation. In your custom error handler (which is where we find ourselves in this post), throw an ErrorException – this then requires you to explicitly catch and handle it in the code that was using the @ before, which is a better way of handling errors.

这篇关于我应该在PHP代码中使用@吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 05:52