
- PHP - Home
- PHP - Roadmap
- PHP - Introduction
- PHP - Installation
- PHP - History
- PHP - Features
- PHP - Syntax
- PHP - Hello World
- PHP - Comments
- PHP - Variables
- PHP - Echo/Print
- PHP - var_dump
- PHP - $ and $$ Variables
- PHP - Constants
- PHP - Magic Constants
- PHP - Data Types
- PHP - Type Casting
- PHP - Type Juggling
- PHP - Strings
- PHP - Boolean
- PHP - Integers
- PHP - Files & I/O
- PHP - Maths Functions
- PHP - Heredoc & Nowdoc
- PHP - Compound Types
- PHP - File Include
- PHP - Date & Time
- PHP - Scalar Type Declarations
- PHP - Return Type Declarations
- PHP - Operators
- PHP - Arithmetic Operators
- PHP - Comparison Operators
- PHP - Logical Operators
- PHP - Assignment Operators
- PHP - String Operators
- PHP - Array Operators
- PHP - Conditional Operators
- PHP - Spread Operator
- PHP - Null Coalescing Operator
- PHP - Spaceship Operator
- PHP Control Statements
- PHP - Decision Making
- PHP - If…Else Statement
- PHP - Switch Statement
- PHP - Loop Types
- PHP - For Loop
- PHP - Foreach Loop
- PHP - While Loop
- PHP - Do…While Loop
- PHP - Break Statement
- PHP - Continue Statement
- PHP Arrays
- PHP - Arrays
- PHP - Indexed Array
- PHP - Associative Array
- PHP - Multidimensional Array
- PHP - Array Functions
- PHP - Constant Arrays
- PHP Functions
- PHP - Functions
- PHP - Function Parameters
- PHP - Call by value
- PHP - Call by Reference
- PHP - Default Arguments
- PHP - Named Arguments
- PHP - Variable Arguments
- PHP - Returning Values
- PHP - Passing Functions
- PHP - Recursive Functions
- PHP - Type Hints
- PHP - Variable Scope
- PHP - Strict Typing
- PHP - Anonymous Functions
- PHP - Arrow Functions
- PHP - Variable Functions
- PHP - Local Variables
- PHP - Global Variables
- PHP Superglobals
- PHP - Superglobals
- PHP - $GLOBALS
- PHP - $_SERVER
- PHP - $_REQUEST
- PHP - $_POST
- PHP - $_GET
- PHP - $_FILES
- PHP - $_ENV
- PHP - $_COOKIE
- PHP - $_SESSION
- PHP File Handling
- PHP - File Handling
- PHP - Open File
- PHP - Read File
- PHP - Write File
- PHP - File Existence
- PHP - Download File
- PHP - Copy File
- PHP - Append File
- PHP - Delete File
- PHP - Handle CSV File
- PHP - File Permissions
- PHP - Create Directory
- PHP - Listing Files
- Object Oriented PHP
- PHP - Object Oriented Programming
- PHP - Classes and Objects
- PHP - Constructor and Destructor
- PHP - Access Modifiers
- PHP - Inheritance
- PHP - Class Constants
- PHP - Abstract Classes
- PHP - Interfaces
- PHP - Traits
- PHP - Static Methods
- PHP - Static Properties
- PHP - Namespaces
- PHP - Object Iteration
- PHP - Encapsulation
- PHP - Final Keyword
- PHP - Overloading
- PHP - Cloning Objects
- PHP - Anonymous Classes
- PHP Web Development
- PHP - Web Concepts
- PHP - Form Handling
- PHP - Form Validation
- PHP - Form Email/URL
- PHP - Complete Form
- PHP - File Inclusion
- PHP - GET & POST
- PHP - File Uploading
- PHP - Cookies
- PHP - Sessions
- PHP - Session Options
- PHP - Sending Emails
- PHP - Sanitize Input
- PHP - Post-Redirect-Get (PRG)
- PHP - Flash Messages
- PHP AJAX
- PHP - AJAX Introduction
- PHP - AJAX Search
- PHP - AJAX XML Parser
- PHP - AJAX Auto Complete Search
- PHP - AJAX RSS Feed Example
- PHP XML
- PHP - XML Introduction
- PHP - Simple XML Parser
- PHP - SAX Parser Example
- PHP - DOM Parser Example
- PHP Login Example
- PHP - Login Example
- PHP - Facebook Login
- PHP - Paypal Integration
- PHP - MySQL Login
- PHP Advanced
- PHP - MySQL
- PHP.INI File Configuration
- PHP - Array Destructuring
- PHP - Coding Standard
- PHP - Regular Expression
- PHP - Error Handling
- PHP - Try…Catch
- PHP - Bugs Debugging
- PHP - For C Developers
- PHP - For PERL Developers
- PHP - Frameworks
- PHP - Core PHP vs Frame Works
- PHP - Design Patterns
- PHP - Filters
- PHP - JSON
- PHP - Exceptions
- PHP - Special Types
- PHP - Hashing
- PHP - Encryption
- PHP - is_null() Function
- PHP - System Calls
- PHP - HTTP Authentication
- PHP - Swapping Variables
- PHP - Closure::call()
- PHP - Filtered unserialize()
- PHP - IntlChar
- PHP - CSPRNG
- PHP - Expectations
- PHP - Use Statement
- PHP - Integer Division
- PHP - Deprecated Features
- PHP - Removed Extensions & SAPIs
- PHP - PEAR
- PHP - CSRF
- PHP - FastCGI Process
- PHP - PDO Extension
- PHP - Built-In Functions
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.