Hudzilla.org - the homepage of Paul Hudson
Contents > Writing PHP > Debugging Wish List | Report Bug | About Me ]

19.8.12     Using @ to disable errors

This is NOT the latest copy of this book; click here for the latest version.

If you find an error message particularly annoying and you are sure it definitely does not apply to you, PHP has a method for you to silence the message entirely. If you place an at symbol, @, before a function that generates an error, PHP will catch the error and silence it entirely. Consider the following two complete scripts:

<?php
    foo
();
?>
<?php
    
@foo();
?>

In script one, the undefined function foo() is called. When this script is run, PHP will halt execution at the undefined function, and print out a fatal error message. However, when the second script is run, PHP will attempt to call the foo() function, fail, but will not halt execution or print out an error - all thanks to the @ symbol before the function call.

While there are legitimate uses for suppressing errors in this way, it is not advised, because it usually works in the same way that sweeping dust under a carpet does not make a house any cleaner - hiding a problem is not the same as solving it! If you explicitly wish to have errors suppressed with @, it is strongly advised that you always write your own code to check return values of functions.

Author's Note: Only use @ if you're absolutely certain the error doesn't apply to you!





<< 19.8.11 Custom exception handlers: set_exception_handler() and restore_exception_handler()   19.8.13 phpinfo() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Be the first to add a comment to this chapter!



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is seven plus one?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow