PHP Learning Note--error

Source: Internet
Author: User

different error handling methods:
    • Simple Die () statement
    • define your own error functions and error triggers
    • Error Reporting
main 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): Assuming the status is a string, the function outputs the string before it is rolled out.

Assume that 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.


Create your own definition 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 report level, error message, send wrong file name, line number. Specify an array (including each variable that is used when an error occurs and their value)

value Constants Descriptive narrative
2 E_warning A non-fatal run-time error. The script runs without pausing.
8 E_notice

Run-time notice.

The script discovers that an error may have occurred, but it may also occur when the script is executing normally.

256 E_user_error A fatal user-generated error. This is analogous to the e_error that the program Ape uses PHP function Trigger_error () settings.
512 E_user_warning A non-fatal user-generated warning. This is analogous to the e_warning that the program Ape uses PHP function Trigger_error () settings.
1024 E_user_notice User-generated notifications. This is analogous to the e_notice that the program Ape uses 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 level e_strict. (In PHP 6.0. E_strict is part of E_all)





function Customerror ($errno, $errstr) {echo "<b>error:</b>[$errno] $errstr <br/>"; 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 the message. and terminates the script. Now. An error handler has been created and we need to determine when to trigger the function.
Set Error Handler:The default error handler for PHP is the mole's error handler. Ability to change error handlers. Make itapply only to certain errorsSo that the script can handle different errors in different ways.
In this case, we intend toAll ErrorsTo use our own custom error handlers.


Set_error_handle ("Customerror"), when all errors are handled. Set_error_handler () requires only one number of parameters. Ability to add a second parameter 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:




























PHP Learning Note--error

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.