Here is how to throw a warning in PHP:
<?php
// trigger a user warning with your $message
trigger_error($message, E_USER_WARNING);
You can throw other errors, like
- E_USER_ERROR: Fatal Error
- E_USER_WARNING: Warning
- E_USER_NOTICE: Notice (default)
You can catch errors and warnings since PHP7. Just catch a Throwable instead of an Exception, like so:
try {
some_function();
} catch (Throwable $e) {
// catch the notice, warning or fatal error
}
The difference between a notice and a warning or fatal error is that a notice won’t stop the execution of the script, but a warning or fatal error will.
Don’t forget to set the error_reporting level to E_ALL, otherwise you won’t see the errors, like so:
error_reporting(E_ALL);