Different error handling methods: Simple die () statement custom error function and error trigger error report
Basic error handling: Using the Die () function
if (!file_exists ("Welcome.txt")) {die ("File not Found"); else{$file =fopen ("Welcome.txt", "R");}
or $file =fopen ("Webdictionary.txt", "R") or Die ("Unable to open file!");
Die (status): If status is a string, the function will output the string before it is rolled. If status is a certificate, this value is used as an exit state. The value of the exit state is between 0 and 254. Exit status 255 is reserved by PHP and will not be used. The status 0 is used to successfully terminate the program.
To create a custom error handler:The function must be capable of handling at least two parameters (Error level and error message), but can accept up to five parameters (optional: File,line-number and the error context). Error_function (error_level,error_message,error_file,error_line,error_context)/Error reporting level, error message, sending the wrong file name, line number, Specify an array (containing each variable that is used when the error occurs and its value)
value |
Constants |
Description |
2 |
E_warning |
Non-fatal run-time error. Script execution is not paused. |
8 |
E_notice |
Run-time notice. The script found that an error might occur, but it may also occur when the script is running correctly. |
256 |
E_user_error |
Fatal user-generated error. This is similar to the e_error that programmers use PHP function Trigger_error () settings.
|
512 |
E_user_warning |
A non-fatal user-generated warning. This is similar to the e_warning that programmers use PHP function Trigger_error () settings.
|
1024 |
E_user_notice |
User-generated notifications. This is similar to the e_notice that programmers use PHP function Trigger_error () settings.
|
4096 |
E_recoverable_error |
A fatal error that can be caught. Similar to E_error, but can be captured by user-defined handlers. (see Set_error_handler ())
|
8191 |
E_all |
All errors and warnings, except for level e_strict. (In PHP 6.0,e_strict is part of E_all) |
function Customerror ($errno, $errstr) {echo
Error:[$errno] $errstr
"; echo "Ending Script"; Die (); }
When the above error is triggered, it gets the error level and error message. It then prints out the error level and message and terminates the script. Now that you have created an error-handling function, we need to determine when the function is triggered.
Set Error Handler:The default error handler for PHP is the mole's error handler. You can modify the error handler so that it applies only to certain errors, so that the script can handle different errors in different ways.
In this case, we intend to use our custom error handlers for all errors.
Set_error_handle ("Customerror"); When all errors are handled, Set_error_handler () requires only one parameter, and a second parameter can be added to specify the error level.
Trigger Error:An error is triggered when the user's input is invalid. Completed by Trigger_error (). $test = 2; if ($test >1) {trigger_error ("Value must be 1 or below")//greater than 1 o'clock trigger error}
possible types of errors:E_USER_ERROR:E_USER_WARNING:E_USER_NOTICE: