Suppress Errors & Warnings With @ operator in PHP

By | November 6, 2011

The at sign (@) is an error control operator in PHP. You can suppress errors, warnings and notices using @ operator in PHP. It is quite common to prefix function calls with @ to suppress errors like this -

@mysqli_connect($host, $user, $pass, $db_name); # If the connection is not established for some reason, then the error will be suppressed
@getimagesize('lol.png'); # If the file is not found, the error will be suppressed

$a = @$b + 5; # Will suppress undefined ($b is not defined) variable notice
echo $a;

In my opinion, you should never make use of @ in your code regardless of the environments (development, production or testing) because it hides all the errors (and not just the annoying notice or such you were trying to hide) not only from display but also from the logs. We usually think that its a quick way to handle the error, but @ basically ignores the error instead of handling it. We should always restore to proper error handling techniques instead of ignoring or suppressing them.

Few months later something might change in the code or the server and the function may start throwing fatal errors. Then you'll have no clue about why your script breaks and never be able to track down the problem. That'll make debugging extremely hard for you and you'll also end up wasting a lot of time.

@ operator is also quite slow as PHP incurs some overhead to suppress the errors, warnings or notices.

You can pretty much always make use of checks like isset() or file_exists() to avoid the notices or warnings that you were trying to hide with @ or else debugging might become a nighmare for you later.

Last Updated On : 6th November 2011

Leave a Reply

Your email address will not be published. Required fields are marked *