The trigger_error Function

The trigger_error function generates a custom error message. Its first parameter is the message text, and the second is the error level (default is E_USER_NOTICE). The function is useful for debugging and creating your own error handling system.

Syntax

phptrigger_error(message, error_level);

Example

Let's generate a simple notice:

php<?php
  trigger_error("This is a notice message");
?>

Code execution result:

phpNotice: This is a notice message in file.php on line 2

Example

Let's generate a custom warning:

php<?php
  trigger_error("Invalid value entered", E_USER_WARNING);
?>

Code execution result:

phpWarning: Invalid value entered in file.php on line 2

Example

Let's generate a fatal error:

php<?php
  trigger_error("Critical system error", E_USER_ERROR);
?>

Code execution result:

phpFatal error: Critical system error in file.php on line 2

Example

Usage in a conditional structure:

php<?php
  $age = -5;
  if ($age < 0) {
    trigger_error("Age cannot be negative", E_USER_WARNING);
  }
?>

Code execution result:

phpWarning: Age cannot be negative in file.php on line 4