note 126049 added to language.operators.errorcontrol
| From: | Toddgreen923 at gmail dot com | Date: | Sun, 02 May 2021 16:13:53 +0000 |
| Subject: | note 126049 added to language.operators.errorcontrol | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-216339@lists.php.net to get a copy of this message | ||
PHP 7.4.18 Released!
Change language:Â Â Â Â Â Â Â Â Â Â
English         Brazilian Portuguese     Â
   Chinese (Simplified)         French   Â
     German         Japanese   Â
     Romanian         Russian   Â
     Spanish         Turkish   Â
     Other      Â
Submit a Pull Request Report a Bug
Error Control Operators ¶
PHP supports one error control operator: the at sign (@). When prepended to an expression in PHP,
any diagnostic error that might be generated by that expression will be suppressed.
If a custom error handler function is set with set_error_handler(), it will still be called even
though the diagnostic has been suppressed, as such the custom error handler should
call error_reporting() and verify that the @ operator was used in the following way:
<?php
function my_error_handler($err_no, $err_msg, $filename, $linenum) {
    if (!(error_reporting() & $err_no)) {
        return false; // Silenced
    }
    // ...
}
?>
Warning
Prior to PHP 8.0.0, the value of the severity passed to the custom error handler was
always 0 if the diagnostic was suppressed. This is no longer the case as of PHP 8.0.0.
Any error message generated by the expression is available in the "message" element
of the array returned by error_get_last(). The result of that function will change on each error,
so it needs to be checked early.
<?php
/* Intentional file error */
$my_file = @file ('non_existent_file') or
    die ("Failed opening file: error was '" . error_get_last()['message'] . "'");
// this works for any expression, not just functions:
$value = @$cache[$key];
// will not issue a notice if the index $key doesn't exist.
?>
Note: The @-operator works only on expressions. A simple rule of thumb is: if one can take
the value of something, then one can prepend the @ operator to it. For instance, it can be
prepended to variables, functions calls, certain language construct calls (e.g. include), and so
forth. It cannot be prepended to function or class definitions, or conditional structures such
as if and foreach, and so forth.
Warning
Prior to PHP 8.0.0, it was possible for the @ operator to disable critical errors that will
terminate script execution. For example, prepending @ to a call of a function which did not
exist, by being unavailable or mistyped, would cause the script to terminate with no indication as
to why.
See Also
error_reporting()
Error Handling and Logging functions
----
Server IP: 45.112.84.4
Probable Submitter: 45.112.85.242 (proxied: 174.253.66.165)
----
Manual Page -- https://php.net/manual/en/language.operators.errorcontrol.php
Edit -- https://main.php.net/note/edit/126049
Del: integrated -- https://main.php.net/note/delete/126049/integrated
Del: useless -- https://main.php.net/note/delete/126049/useless
Del: bad code -- https://main.php.net/note/delete/126049/bad+code
Del: spam -- https://main.php.net/note/delete/126049/spam
Del: non-english -- https://main.php.net/note/delete/126049/non-english
Del: in docs -- https://main.php.net/note/delete/126049/in+docs
Del: other reasons-- https://main.php.net/note/delete/126049
Reject -- https://main.php.net/note/reject/126049
Search -- https://main.php.net/manage/user-notes.php