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
trigger_error(message, error_level);
Example
Let's generate a simple notice:
<?php
trigger_error("This is a notice message");
?>
Code execution result:
Notice: This is a notice message in file.php on line 2
Example
Let's generate a custom warning:
<?php
trigger_error("Invalid value entered", E_USER_WARNING);
?>
Code execution result:
Warning: Invalid value entered in file.php on line 2
Example
Let's generate a fatal error:
<?php
trigger_error("Critical system error", E_USER_ERROR);
?>
Code execution result:
Fatal error: Critical system error in file.php on line 2
Example
Usage in a conditional structure:
<?php
$age = -5;
if ($age < 0) {
trigger_error("Age cannot be negative", E_USER_WARNING);
}
?>
Code execution result:
Warning: Age cannot be negative in file.php on line 4