In PHP, user_error() is an alias of trigger_error(). It creates your own error message at a level you choose, and it behaves exactly the same way.
An alias is just a second name for the same function. You can write user_error() or trigger_error() and get identical results. Most modern code uses trigger_error(), but you may see user_error() in older projects, so it is good to know that they are the same thing.
Syntax
Call user_error() with the values you need. Start with this line.
PHP
user_error("Something is wrong", E_USER_WARNING);
Call
What it does
user_error("Cache is empty")
Raises an E_USER_NOTICE, which is the default level.
user_error("Disk is almost full", E_USER_WARNING)
Raises a user warning for a problem that needs attention.
user_error() and trigger_error() are the same function under two names. The level must be E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE or E_USER_DEPRECATED. Since PHP 8.4, E_USER_ERROR is deprecated.
Library declarations
PHP
user_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 — Using user_error()
A handler prints the level number and the message, so you can see user_error() working.
Deprecated: oldFunction() is deprecated, use newFunction()
old result
How It Works
oldFunction() first raises a deprecation notice with user_error().
The handler receives it and prints the message.
The function does not stop, so it continues and returns its result.
The echo prints old result after the deprecation message.
Frequently Asked Questions
It raises your own error at a level you choose, for example a notice, warning or deprecation. It is an alias of trigger_error().
Yes. They are the same function with two names, with the same parameters and the same behavior.
Use trigger_error() in new code. It is the name that most documentation and tutorials use, so other developers will recognize it more quickly.
E_USER_NOTICE (the default), E_USER_WARNING, E_USER_DEPRECATED and E_USER_ERROR. Other levels throw a ValueError, and E_USER_ERROR is deprecated since PHP 8.4.