PHP trigger_error() Function - GeeksforGeeks

PHP trigger_error() Function

Last Updated : 30 Oct, 2026

The trigger_error() function in PHP generates a user-level error message when a specified condition occurs. It allows developers to report errors, warnings, or notices in their code using predefined error-level constants.

  • The function accepts an error message and an optional error level.
  • The default error level is E_USER_NOTICE.
  • It returns true when the error is triggered successfully.
  • The maximum length of the error message is 1,024 bytes.

Syntax:

trigger_error(string $message, int $error_level = E_USER_NOTICE);
  • $message: Specifies the error message. Its maximum length is 1,024 bytes; additional characters are truncated.
  • $error_level: Specifies the error level. It accepts E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE, or E_USER_DEPRECATED. If omitted, it defaults to E_USER_NOTICE.

Example 1: Trigger a User-Level Notice

The following example uses trigger_error() to generate a notice when a value is not numeric.

PHP
<?php
function checkValue($value) {
    if (is_numeric($value)) {
        echo $value . " is numeric";
    } else {
        trigger_error("Value must be numeric", E_USER_NOTICE);
    }
}

checkValue("GFG");
?>

The exact output may also include the filename and line number, depending on the PHP configuration.

Example 2: Trigger a User-Level Warning

The following example generates a warning when the divisor is zero.

PHP
<?php
function divide($first, $second) {
    if ($second == 0) {
        trigger_error("Cannot divide by zero", E_USER_WARNING);
        return;
    }

    echo $first / $second;
}

divide(10, 0);
?>

The E_USER_WARNING constant generates a user-level warning. The return statement prevents the division from being performed after the warning is triggered.

Common Error Levels Used with trigger_error()

ConstantDescription
E_USER_NOTICEGenerates a user-level notice. This is the default error level.
E_USER_WARNINGGenerates a user-level warning.
E_USER_DEPRECATEDGenerates a user-level deprecation notice.
Comment