In PHP, trigger_error() creates your own error message at a level you choose, as if PHP itself had raised it.
It is the way to say: something is not right here. You pick a level such as E_USER_NOTICE, E_USER_WARNING or E_USER_DEPRECATED. If you have set an error handler with set_error_handler(), it receives your message. Otherwise PHP prints it or logs it according to its settings.
Syntax
Call trigger_error() with the values you need. Start with this line.
PHP
trigger_error("Something is wrong", E_USER_WARNING);
Call
What it does
trigger_error("Cache is empty")
Raises an E_USER_NOTICE, which is the default level.
trigger_error("Disk is almost full", E_USER_WARNING)
Raises a user warning for a problem that needs attention.
Tells developers that a feature is old and will go away.
user_error() is an alias of trigger_error() and behaves the same way. The level must be one of E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE or E_USER_DEPRECATED. Since PHP 8.4, passing E_USER_ERROR is deprecated, so throw an exception for serious problems instead.
Library declarations
PHP
trigger_error(string $message, int $error_level = E_USER_NOTICE): true
Parameters
Parameter
Description
message
Required. The text of the error message. It is limited to 1024 bytes, and anything longer is cut off.
error_level
Optional. The error level. Use E_USER_NOTICE (the default), E_USER_WARNING, E_USER_DEPRECATED or E_USER_ERROR. Any other value throws a ValueError.
Return Value
Return Value
Description
true
Always returns true when the call succeeds.
PHP Examples
Example 1 — The Three Common Levels
A handler prints which level was used, so you can see how the default and the other levels differ.
The @ operator hides the notice from the screen, but trigger_error() still returns true.
var_dump() prints bool(true), and error_get_last() still remembers the message Quiet.
E_WARNING is a level for PHP itself, so it is not allowed in trigger_error().
PHP throws a ValueError, and the catch block prints its class name.
Frequently Asked Questions
It raises your own error at a level you choose, such as a notice, warning or deprecation. A custom error handler can receive it, or PHP shows or logs it as usual.
E_USER_NOTICE (the default), E_USER_WARNING, E_USER_DEPRECATED and E_USER_ERROR. Other levels throw a ValueError. E_USER_ERROR is deprecated since PHP 8.4.
trigger_error() reports a problem and the script normally carries on. throw creates an exception that stops normal flow until it is caught. Use exceptions for problems the caller must handle.
None. user_error() is just another name for trigger_error(). Both work in exactly the same way.
Not for notices, warnings and deprecations. The script continues after the call. Only E_USER_ERROR stops the script when no handler deals with it, and that level is deprecated in PHP 8.4.