
- 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 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.