gpt4 book ai didi

php - 我应该在 PHP 代码中使用 @ 吗?

转载 作者:行者123 更新时间:2023-12-03 00:26:13 27 4
gpt4 key购买 nike

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

最佳答案

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

特别是“@有其用途”部分:

Now one really should use the @ operator very sparingly, handling errors instead of suppressing them. But there are a small number of situations I can think of where one might need to suppress some PHP errors. Let me offer two examples :

  • 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.

  • 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 - 我应该在 PHP 代码中使用 @ 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4872340/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com