trigger_error
Raises a user-level warning or notice.
Syntax
trigger_error(string $message, int $error_level = E_USER_NOTICE): trueParameters
| Parameter | Type | Description |
|---|---|---|
| $message | string | The message text. |
| $error_level | int | Optional. `E_USER_NOTICE`, `E_USER_WARNING`, `E_USER_DEPRECATED`. |
Return value
trueAlways `true`.
Examples
A message about an outdated function:
<?php
set_error_handler(function (int $level, string $message) {
echo 'Caught: ', $message;
return true;
});
function oldPrice(): int
{
trigger_error('oldPrice() is outdated, use price()', E_USER_DEPRECATED);
return 100;
}
oldPrice();Output:
Caught: oldPrice() is outdated, use price()Notes
Unlike an exception, the message does not stop the program. For real errors throw exceptions.
See also
- set_error_handler — Sets a custom function to handle PHP warnings and notices.
- error_log — Writes a message to the error log.
- Exception — The base class of exceptions.
In the tutorial
- Errors and debugging — The kinds of PHP errors, error display settings, the log, turning warnings into exceptions and debugging techniques.