When not to suppress fatal errors with @ in PHP

In PHP the @ is used to suppress warning or error messages, but it has some serious drawbacks and you should not use it randomly.

It hides the bugs

One of the biggest disadvantages of @ is it hides the useful informations about the running application.

Here is an example, one of your AJAX response empty, so you try to debug it, you added several echo statements, you found that a method invocation failed to return, all the code after the invocation is not executed.

If you are lucky, the method is in your own code base, you can inspect it. It will be worse if the method comes from third party library.

Finally you located the code that something like this

Remove the @ operator, you can see the error in the response

 
<b>Fatal error</b>:  Call to undefined function mysqli_connect_error() in 
 

Now you find the root of the bug: you didn't enable the mysqli support in PHP configuration file.

It breaks AJAX response

If you specified the response format of AJAX as JSON, when a fatal error happens, the server will be interrupted and returns ahead of time.

If you output JSON in last line of your code with json_encode, it will not be executed and the Javascript get empty response.