PHP Learning Notes--error_php Tutorial

Source: Internet
Author: User

PHP Learning Note--error


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 the status is a string, the function will output the string before it is rolled out. If status is a certificate, this value is used as the exit status. The value of the exit status is between 0 and 254. Exit status 255 is reserved by PHP and will not be used. 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 Error context). Error_function (Error_level,error_message,error_file,error_line,error_context)//Error reporting level, error message, sending wrong file name, line number, Specifies an array that contains each variable that is used when the error occurred and their value
value Constants Description
2 E_warning A non-fatal run-time error. Script execution is not paused.
8 E_notice

Run-time notice. The script discovers that an error may have occurred, but it may also occur when the script is running correctly.

256 E_user_error A fatal user-generated error. This is similar to the e_error that programmers use to set the PHP function Trigger_error ().
512 E_user_warning A non-fatal user-generated warning. This is similar to the e_warning that programmers use to set the PHP function Trigger_error ().
1024 E_user_notice User-generated notifications. This is similar to the e_notice that programmers use to set the PHP function Trigger_error ().
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 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 ";d ie ();}
When the above error is triggered, it gets the error level and error message. It then outputs the error level and message, and terminates the script. Now that we have created an error handler, we need to determine when the function is fired.
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 example, 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 level of error.
Trigger Error:An error is triggered when the user's input is not valid. Completed by Trigger_error (). $test =2;if ($test >1) {trigger_error ("Value must be 1 or below");//greater than 1 o'clock triggering error}
possible types of errors:E_USER_ERROR:E_USER_WARNING:E_USER_NOTICE:




























http://www.bkjia.com/PHPjc/851379.html www.bkjia.com true http://www.bkjia.com/PHPjc/851379.html techarticle PHP Learning note--error different error handling methods: Simple die () statement custom error function and error trigger error report basic error handling: Using the Die () function if (!file_exis ...

  • Related Article

    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.