Php learning notes error

Source: Internet
Author: User
Php learning notes:

  • Simple die () statement
  • Custom error functions and triggers
  • 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 ("mermerror ");

    When processing all errors, set_error_handler () only requires one parameter. you can add the 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:

    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.