PHP: Trigger fatal error?
E_USER_ERROR is the suited constant.
trigger_error("Fatal error", E_USER_ERROR);
See also the first example of the manual page and the list of PHP Errors (only ones beginning with E_USER* may be issued from trigger_error).
Note: if you're using a custom error handler (see set_error_handler)
E_USER_ERROR
will NOT halt/exit/die unless the error handler returns false
nutshell : your custom error handler effectively determines if E_USER_ERROR
is treated as a fatal
If you are using PHP 7 or higher, Error class works too:
$flag = false;
try {
if ($flag == false) {
throw new Error('An error occured');
}
} catch (Error $e) {
echo $e->getMessage();
}
If you put throw new Error('An error occured');
outside the Try Catch
, You will get a Fatal Error.
debug_print_backtrace();
trigger_error("As much information as you can provide, please", E_USER_ERROR);
exit();
exit() terminates the PHP script entirely. The more information you can provide users or developers about the error, the better. Error codes are passé.
Edit: use of E_USER_ERROR should terminate the script anyway, so moved debug_print_backtrace() before trigger_error().