How to shield the runtime exception of the specified code in PHP


Error control operator

Use @ to block errors

PHP supports an error control operator: @. When it is placed before a PHP expression, any error messages that the expression may produce will be ignored. If a custom error handling function is set with set_error_handler(), it will still be called. Refer to the sample code below for usage.

            $my_file = @file ('non_existent_file');
$value = @$cache[$key];


This operator is only valid for expressions. A simple rule for novices is: if you can get a value from somewhere, you can add the @ operator in front of it. For example, you can put it before variables, functions and include calls, constants, etc. It cannot be placed before the definition of a function or class, nor can it be used in conditional structures such as if and foreach.