
- PHP Tutorial
- PHP - Home
- PHP - Introduction
- PHP - Environment Setup
- PHP - Syntax Overview
- PHP - Variable Types
- PHP - Constants
- PHP - Operator Types
- PHP - Decision Making
- PHP - Loop Types
- PHP - Arrays
- PHP - Strings
- PHP - Web Concepts
- PHP - GET & POST
- PHP - File Inclusion
- PHP - Files & I/O
- PHP - Functions
- PHP - Cookies
- PHP - Sessions
- PHP - Sending Emails
- PHP - File Uploading
- PHP - Coding Standard
- Advanced PHP
- PHP - Predefined Variables
- PHP - Regular Expression
- PHP - Error Handling
- PHP - Bugs Debugging
- PHP - Date & Time
- PHP & MySQL
- PHP & AJAX
- PHP & XML
- PHP - Object Oriented
- PHP - For C Developers
- PHP - For PERL Developers
- PHP Form Examples
- PHP - Form Introduction
- PHP - Validation Example
- PHP - Complete Form
- PHP login Examples
- PHP - Login Example
- PHP - Facebook Login
- PHP - Paypal Integration
- PHP - MySQL Login
- PHP AJAX Examples
- PHP - AJAX Search
- PHP - AJAX XML Parser
- PHP - AJAX Auto Complete Search
- PHP - AJAX RSS Feed Example
- PHP XML Example
- PHP - XML Introduction
- PHP - Simple XML
- PHP - Simple XML GET
- PHP - SAX Parser Example
- PHP - DOM Parser Example
- PHP Frame Works
- PHP - Frame Works
- PHP - Core PHP vs Frame Works
- PHP Design Patterns
- PHP - Design Patterns
- PHP Function Reference
- PHP - Built-In Functions
- PHP Useful Resources
- PHP - Questions & Answers
- PHP - Useful Resources
- PHP - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
PHP - Function set_error_handler()
Syntax
mixed set_error_handler ( callback $error_handler [, int $error_types] );
Definition and Usage
This function can be used for defining your own way of handling errors during runtime, for example in applications in which you need to do cleanup of data/files when a critical error happens, or when you need to trigger an error under certain conditions.
Parameters
Sr.No | Parameter & Description |
---|---|
1 | error_handler(Required) It specifies the function to be run at errors. Syntax of error_handler is given below. |
2 |
error_types(Optional) It specifies on which errors report levels the user-defined error will be shown. Default is "E_ALL". See "PHP Error and Logging Constants:" for possible error report levels. |
Error Handler Function Syntax
error_function(error_level,error_message, error_file,error_line,error_context);
Here are the paramenter's description −
errno − The first parameter, errno, contains the level of the error raised, as an integer.
errstr − The second parameter, errstr, contains the error message, as a string.
errfile − The third parameter is optional, errfile, which contains the filename that the error was raised in, as a string.
errline − The fourth parameter is optional, errline, which contains the line number the error was raised at, as an integer.
errcontext − The fifth parameter is optional, errcontext, which is an array that points to the active symbol table at the point the error occurred.
Return Value
Returns a string containing the previously defined error handler (if any), or NULL on error.
Example
Following is the usage of this function −
<?php function customError($errno, $errstr, $errfile, $errline) { echo "Custom error: [$errno] $errstr\n"; echo "Error on line $errline in $errfile\n"; echo "Ending Script"; die(); } //set error handler set_error_handler("customError"); $test = 0; //trigger error if ($test > -1) { trigger_error("A custom error has been triggered"); } ?>
This will produce the following result −
Custom error: [1024] A custom error has been triggered Error on line 16 in /home/cg/root/1531703/main.php Ending Script