Error Control Operator No Longer Suppress Fatal Errors in PHP 8.0

Error Control Operator No Longer Suppress Fatal Errors in PHP 8.0

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.ConstantDescription
1.E_ERRORFatal run-time errors.
2.E_CORE_ERRORFatal errors that occurs in PHP's initial startup.
3.E_COMPILE_ERRORFatal compile-time errors.
4.E_USER_ERRORErrors that generated by user using trigger_error function.
5.E_RECOVERABLE_ERRORCatchable fatal error.
6.E_PARSECompile-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

Your email address will not be published.