Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

trigger_error

Raises a user-level warning or notice.

Syntax

trigger_error(string $message, int $error_level = E_USER_NOTICE): true

Parameters

ParameterTypeDescription
$messagestringThe message text.
$error_levelintOptional. `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

In the tutorial

  • Errors and debugging — The kinds of PHP errors, error display settings, the log, turning warnings into exceptions and debugging techniques.

Thanks for reading!

Donate to the site — we work hard for you.