PHP trigger_error() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Error

Definition

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);
CallWhat 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.
trigger_error("Use newFunction() instead", E_USER_DEPRECATED)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

ParameterDescription
messageRequired. The text of the error message. It is limited to 1024 bytes, and anything longer is cut off.
error_levelOptional. 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 ValueDescription
trueAlways 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.

PHP
<?php
set_error_handler(function (int $errno, string $errstr): bool {
    $names = [
        E_USER_NOTICE => 'NOTICE',
        E_USER_WARNING => 'WARNING',
        E_USER_DEPRECATED => 'DEPRECATED'
    ];

    echo $names[$errno] ?? 'OTHER', ": ", $errstr, PHP_EOL;
    return true;
});

trigger_error("Default level");
trigger_error("Be careful", E_USER_WARNING);
trigger_error("Old way", E_USER_DEPRECATED);

How It Works

  1. The handler maps each level constant to a readable name and prints it with the message.
  2. trigger_error() with only a message uses the default level, E_USER_NOTICE.
  3. The second call passes E_USER_WARNING, and the third passes E_USER_DEPRECATED.
  4. The handler returns true each time, so PHP does not print anything on its own.

Example 2 — Warning About Bad Input

A function can raise a warning for bad input and still return normally, so the script does not stop.

PHP
<?php
set_error_handler(function (int $errno, string $errstr): bool {
    echo "Warning caught: ", $errstr, PHP_EOL;
    return true;
});

function setAge(int $age): bool {
    if ($age < 0) {
        trigger_error("Age cannot be negative", E_USER_WARNING);
        return false;
    }

    return true;
}

var_dump(setAge(-5));
var_dump(setAge(30));

How It Works

  1. setAge() checks whether the age is below zero.
  2. For -5 it raises a user warning and returns false, and our handler prints the message.
  3. For 30 there is no problem, so no warning is raised and the function returns true.
  4. The script never stops, because a warning is not fatal.

Example 3 — Return Value and Invalid Levels

trigger_error() returns true. Passing a level that is not allowed throws a ValueError in PHP 8.

PHP
<?php
$result = @trigger_error("Quiet", E_USER_NOTICE);

var_dump($result);
echo error_get_last()['message'], PHP_EOL;

try {
    trigger_error("Wrong level", E_WARNING);
} catch (ValueError $e) {
    echo get_class($e), PHP_EOL;
}

How It Works

  1. The @ operator hides the notice from the screen, but trigger_error() still returns true.
  2. var_dump() prints bool(true), and error_get_last() still remembers the message Quiet.
  3. E_WARNING is a level for PHP itself, so it is not allowed in trigger_error().
  4. 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.

Next: user_error()

user_error() is an alias of trigger_error(). It creates your own error message at a level .

user_error() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful