PHP supports error control operator (@
) which can be added to an expression beginning. This operator suppresses notices, warnings, and errors that might be generated by the expression.
In versions prior to PHP 8.0, the @
operator allows suppressing any error messages. Since PHP 8.0, the @
operator no longer suppress fatal errors. This includes these types of errors:
No. | Constant | Description |
---|---|---|
1. | E_ERROR | Fatal run-time errors. |
2. | E_CORE_ERROR | Fatal errors that occurs in PHP's initial startup. |
3. | E_COMPILE_ERROR | Fatal compile-time errors. |
4. | E_USER_ERROR | Errors that generated by user using trigger_error function. |
5. | E_RECOVERABLE_ERROR | Catchable fatal error. |
6. | E_PARSE | Compile-time parse errors. |
Let's say we have a function that generates a user-defined error. A function is called by adding the @
operator at the beginning of a function name.
<?php
function run() {
trigger_error('Not implemented', E_USER_ERROR);
}
@run();
echo 'This message wouldn\'t be printed';
In versions prior to PHP 8.0, the code snippet does not emit any errors because a user-defined error was suppressed by the @
operator. However, code execution will be stopped.
In PHP 8.0 or newer versions, a user-defined error is not suppressed, and a code snippet emits a fatal error. Of course, code execution will be stopped.
Fatal error: Not implemented in main.php on line 4
Note that the @
operator continue to suppress warnings and notices.
Leave a Comment
Cancel reply