PHP Reference ยท Operators
The PHP @ error control operator
Reference for the PHP @ operator: how it silences diagnostics, what it no longer hides in PHP 8, its performance cost and better alternatives.
The @ operator placed in front of an expression silences the warnings, notices and deprecations that expression would produce. Execution continues as usual and you get whatever value the expression returns, often false or null.
It hides information instead of handling it. Use it almost never: check the condition up front, use a function that reports failure cleanly, or catch an exception. If an error is genuinely expected and cannot be tested for, @ with an explicit check of the result is acceptable.
$text = @file_get_contents("maybe-missing.txt"); if ($text === false) { /* handle it */ }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
@expression
Result: the value of the expression. The last silenced diagnostic is still available from error_get_last().
Usage notes
- Fatal errors and exceptions are not suppressed.
@intdiv(1, 0)still throwsDivisionByZeroError. In PHP 8@also no longer hides fatal errors. - Custom error handlers still run. A handler set with
set_error_handler()is called even for silenced errors; inside it, checkerror_reporting() & $errnobefore reacting. Since PHP 8error_reporting()returns a reduced mask (4437) while@is active. - It costs time.
@temporarily changeserror_reporting, so avoid it in hot loops. - It hides real bugs. An undefined variable or index quietly produces
null, and the typo stays unnoticed. Useisset(),??orarray_key_exists(). - Better alternatives:
??for missing keys,is_readable()orfile_exists()before file functions,JSON_THROW_ON_ERRORfor JSON,filter_var()for validation, andtry/catchfor exceptions. - It binds tightly.
@$a + $bsilences only$a. Put it directly in front of the call you want quiet. - Never use it to hide errors in production. Configure logging (
log_errors) and turndisplay_errorsoff instead.