Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use @ in my PHP code?

Tags:

php

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

like image 451
Gaurav Avatar asked Feb 02 '11 08:02

Gaurav


People also ask

What is the use of symbol in PHP?

It makes PHP suppress any error messages (notice, warning, fatal, etc) generated by the associated expression. It works just like a unary operator, for example, it has a precedence and associativity.

Is PHP still relevant in 2020?

It is true that PHP has slipped down the rankings of the most popular programming languages, from 5th in 2017 to 8th in 2020 as per the Stack Overflow annual developer survey. And yet, PHP continues to be used by nearly 80% of all websites, powering some major platforms like Wordpress and Facebook.

Should you still use PHP?

However, statistics show that around 80% of websites are still written on PHP. The language is still relevant and popular for web development, as it's easy, fast, constantly updated and there is a wide market of specialists who can work with it.


2 Answers

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":

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.

like image 198
Haim Evgi Avatar answered Nov 15 '22 21:11

Haim Evgi


You should not use the error suppression operator.

In a production environment, no PHP error messages should be shown to the user. They are not useful, because they are full of technical details and do not tell the user how to proceed. Instead, log the error and show your own error message.

In a development environment, all PHP error messages should be shown to the user. They are a vital clue as to the cause of the problem and should be noticed early.

Use the Errors and Logging Configuration Options to distinguish between theses two. Performance is not a useful criterion to decide whether to use @ or not.

like image 43
Oswald Avatar answered Nov 15 '22 20:11

Oswald