PHP Errors (Cont.)
Creating a Custom Error Handler
A custom error handler creates a special function that can be called when an error occurs in PHP.
This example checks whether the given URL starts with the “http://.”
It uses the following functions:
- The functions
strncmp
compares two strings and header
sends a raw HTTP header to a client.
- The function
trigger_error
is used to trigger an error message at a user-specified condition.
- The function
set_error_handler
sets a user-defined function to handle errors.
It needs one parameter and a second parameter could be added to specify an error level as below.
These error report levels are the different types of error the user-defined error handler can be used for:
Value |
Constant |
Description |
2 |
E_WARNING |
Non-fatal run-time errors.
Execution of the script is not halted. |
8 |
E_NOTICE |
Run-time notices.
The script found something that might be an error, but could also happen when running a script normally. |
256 |
E_USER_ERROR |
Fatal user-generated error.
This is like an E_ERROR set by the programmer using the PHP function trigger_error . |
512 |
E_USER_WARNING |
Non-fatal user-generated warning.
This is like an E_WARNING set by the programmer using the PHP function trigger_error . |
1024 |
E_USER_NOTICE |
User-generated notice.
This is like an E_NOTIC set by the programmer using the PHP function trigger_error . |
4096 |
E_RECOVERABLE_ERROR |
Catchable fatal error.
This is like an E_ERROR but can be caught by a user defined handle. |
8191 |
E_ALL |
All errors and warnings, except level E_STRICT . |