Php study notes -- error_PHP tutorial

Source: Internet
Author: User
Php learning notes -- error. Php learning notes -- different error handling methods: Simple die () statement custom error function and error trigger error report basic error handling: Use the die () function if (! File_exis php study notes -- error
Different error handling methods: Simple die () statement custom error function and error trigger error report Basic error handling: Use 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 release. If status is a certificate, this value is used as the exit status. The exit status ranges from 0 to 254. Exit status 255 is retained by php and will not be used. Status 0 is used to successfully terminate the program.
Create a custom error processor:This function must be able to process at least two parameters (error level and error message), but can accept up to five parameters (optional: file, line-number, and error context ). Error_function (error_level, error_message, error_file, error_line, error_context) // error report level, error message, error file name sent, row number, define an array (containing each variable and their values when an error occurs)
Value Constant Description
2 E_WARNING Non-fatal run-time error. Do not pause script execution.
8 E_NOTICE

Run-time notification. An error may occur when the script runs normally.

256 E_USER_ERROR Fatal user-generated error. This is similar to the E_ERROR set by the programmer using the PHP function trigger_error.
512 E_USER_WARNING Non-fatal user-generated warning. This is similar to the E_WARNING set by the programmer using the PHP function trigger_error.
1024 E_USER_NOTICE User-generated notifications. This is similar to the E_NOTICE set by the programmer using the PHP function trigger_error.
4096 E_RECOVERABLE_ERROR Possible fatal errors. Similar to E_ERROR, but can be captured by a user-defined handler. (See set_error_handler ())
8191 E_ALL All errors and warnings except 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 will get the error level and error message. It then outputs the error level and message and terminates the script. Now we have created an error handling function. we need to determine when to trigger this function.
Set Error Handler:Php's default error handler is an internal error handler. You can modify the error handler so that it can only be applied to some errors, so that the script can handle different errors in different ways.
In this example, we plan to use our custom error handler for all errors.
Set_error_handle ("customError"); when processing all errors, set_error_handler () only requires one parameter. you can add a second parameter to specify the error level.
Trigger error:An error is triggered when user input is invalid. Trigger_error. $ Test = 2; if ($ test> 1) {trigger_error ("Value must be 1 or below"); // An error is triggered when the Value is greater than 1}
Possible error types:E_USER_ERROR: E_USER_WARNING: E_USER_NOTICE:




























Different methods for handling errors: Simple die () statement custom error function and error trigger error report basic error handling: Use the die () function if (! File_exis...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.