PHP Error Handling trigger_error() Function



The PHP Error Handling trigger_error() function is used to create custom error messages. Developers can use it to show errors when something unexpected happens in their code. When resolving or debugging particular program errors, this is useful. Whether the error is an alert or a warning is up to you. The functionality makes it easier to find and fix problems quickly.

It improves programming quality by giving clear error alerts. The trigger_error() function can help you make your application simpler and maintainable.

Syntax

Below is the syntax of the PHP Error Handling trigger_error() function −

bool trigger_error ( string $message, int $error_level )

Parameters

Here are the parameters of the trigger_error() function −

  • $message: (Required) The error message associated with this problem. It can be up to 1024 bytes long. Characters will be shortened if they are longer than 1024 bytes.

  • $error_level: (Optional) The error type attributed to this error. It only works with constants in the E_USER_* family and has E_USER_NOTICE as its default.

Here are the possible Error Level Types −

  • E_USER_ERROR: Fatal user-generated run-time error. Errors that can not be recovered from. Execution of the script is halted.

  • E_USER_WARNING: Non-fatal user-generated run-time warning. Execution of the script is not halted.

  • E_USER_NOTICE: Default. User-generated run-time notice. The script found something that might be an error, but could also happen when running a script normally.

Return Value

The trigger_error() function returns TRUE on success. And FALSE on failure.

PHP Version

First introduced in core PHP 4.0.1, the trigger_error() function continues to function easily in PHP 5, PHP 7, and PHP 8.

Example 1

This program shows a simple way to use the PHP Error Handling trigger_error() method to display a custom error message. It gives a notice-level error by default.

<?php
    // Display a basic custom error message
    trigger_error("This is a custom error message.");
?>

Output

Here is the outcome of the following code −

Notice: This is a custom error message.

Example 2

In the below PHP code we will use the trigger_error() function and to generate a warning with the help of the E_USER_WARNING error level. So basically we are using the optional parameter $error_level here.

<?php
    // Generate a warning-level error message
    trigger_error("This is a custom warning message.", E_USER_WARNING);
?> 

Output

This will generate the below output −

Warning: This is a custom warning message.

Example 3

Now in the below code we will try to trigger an error message based on the the given condition. So here the function is used inside a condition to trigger an error if the variable does not meet the given criteria.

<?php
   $value = 5;
   // Trigger an error message if the value is less than 10
   if ($value < 10) {
      trigger_error("Value must be 10 or greater.", E_USER_ERROR);
   }
?> 

Output

This will create the below output −

Fatal error: Value must be 10 or greater.

Example 4

In the following example, we are using the trigger_error() function for debugging purposes by logging errors when the given actions fail.

<?php
   function divide($a, $b) {
      // Check if divisor is zero and trigger an error
      if ($b == 0) {
          trigger_error("Division by zero is not allowed.", E_USER_ERROR);
          return false;
      }
      return $a / $b;
   }
   
   // Call the function with a zero divisor
   $result = divide(10, 0);
?> 

Output

Following is the output of the above code −

Fatal error: Division by zero is not allowed. 
php_function_reference.htm
Advertisements