3v4l.org

run code in 300+ PHP versions simultaneously
<?php set_error_handler(function (int $no, string $str): bool { var_dump('DEBUG: ' . $str); global $level; if ($level !== null) { error_reporting($level); // restore error level unset($level); return true; // handle the internal/helper error } // supress the current error output $level = error_reporting(); error_reporting(0); trigger_error('internal'); // trigger internal/helper error to restore the error level return false; // not handled to set the error_get_last() }); error_reporting(E_ALL); // to show the error, has no effect if the handler is called or not (expected, documented) var_dump(error_reporting()); trigger_error('custom'); // trigger_error('internal2'); // uncomment to reproduce the desired behaviour var_dump(error_get_last()['message']); var_dump(error_reporting());
Output for 8.4.1 - 8.4.12
int(30719) string(13) "DEBUG: custom" string(6) "custom" int(0)
Output for 8.1.25 - 8.1.33, 8.2.12 - 8.2.29, 8.3.0 - 8.3.25
int(32767) string(13) "DEBUG: custom" string(6) "custom" int(0)

preferences:
40.08 ms | 407 KiB | 5 Q