PHP Error Handling user_error() Function
The PHP Error Handling user_error() function is used to generate a user-defined error message. It is used to display an error message when a user-specified condition is met. It can be used in combination with the built-in error handler or a user-defined function defined using the set_error_handler() method.
When you want to execute a script dynamically with a user-defined message to a given condition, this function is very useful. This function returns TRUE otherwise, and FALSE if an invalid error type is specified.
This function works the same as the trigger_error() function.
Syntax
Below is the syntax of the PHP Error Handling user_error() function −
bool user_error ( string $error_msg [, int $error_type] );
Parameters
Here are the parameters of the user_error() function −
$error_msg − (Required) It specifies the error message. Limited to 1024 characters in length.
$error_types − (Optional) It specifies the error type for this error message. Possible error 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 user_error() function returns TRUE on success. And FALSE if wrong error_type is specified.
PHP Version
First introduced in core PHP 4, the user_error() function continues to function easily in PHP 5, PHP 7, and PHP 8.
Example 1
This program demonstrates the simple use of the PHP Error Handling user_error() function to display a user-defined warning message. It starts an informational message that is not necessary that does not stop the script.
<?php
// Display a simple notice
$message = "This is a user-defined notice.";
user_error($message, E_USER_NOTICE);
echo "Script continues after the notice.";
?>
Output
Here is the outcome of the following code −
Notice: This is a user-defined notice. Script continues after the notice.
Example 2
This program shows how to generate a warning with the help of the user_error() function. The warning is displayed to the user, but the script will still run. Basically we are using the optional parameter $error_types E_USER_WARNING which is used to given user generated run time error.
<?php
// Trigger a warning
$age = 15;
if ($age < 18) {
user_error("Warning: Age must be 18 or older to proceed.", E_USER_WARNING);
}
echo "Script continues after the warning.";
?>
Output
This will generate the below output −
Warning: Warning: Age must be 18 or older to proceed. Script continues after the warning.
Example 3
Now the below code uses the E_USER_ERROR inside the user_error() method, and create a critical error message. The program execution stops when the error is triggered.
<?php
// Trigger a fatal error
$divisor = 0;
if ($divisor == 0) {
user_error("Error: Division by zero is not allowed.", E_USER_ERROR);
}
// This line will not execute because of the fatal error
echo "This message won't be displayed.";
?>
Output
This will create the below output −
Error: Division by zero is not allowed.
Example 4
In the following example, we are using the user_error() function and combining it with a custom error handler. This program uses user_error() function to handle errors in a user-defined way and set_error_handler() to set a custom error handler.
<?php
// Custom error handler function
function customErrorHandler($errno, $errstr) {
echo "Custom Error [$errno]: $errstr\n";
return true;
}
// Set the custom error handler
set_error_handler("customErrorHandler");
// Triggering a user-defined error
user_error("This is a custom-handled error.", E_USER_WARNING);
echo "Script continues after custom error handling.";
?>
Output
Following is the output of the above code −
Custom Error [512]: This is a custom-handled error. Script continues after custom error handling.