How to ignore @ errors in a custom PHP error handler

In my PHP pages, I’m using a custom error handler, to make potential errors a bit more user-friendly. However, in some very particular cases, I don’t want an error to be raised (e.g. I’m logging all errors to a server file, but if writing to that file does not work for whatever reason, I don’t want to bother the user with it). In those cases, I’m using the @ error-control operator to ignore any error:
@error_log($error_trace.' ['.str_replace("\n",'\n',$error_message).'] ['.str_replace("\n",'\n',$error_data)."]\n",3,ERROR_LOG);

The @ error-control operator, However, even with @, the custom error handler is still triggered.

Thanks to this blog post I learned that error_reporting is 0 if the statement that caused the error was prepended by the @ error-control operator. So the solution is to add:
if (error_reporting() === 0) {
// continue script execution, skipping standard PHP error handler  (e.g.when using @ in front of an expression)
return true;
}

to the custom error handler

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.