How to use PHP to customize error handling functions for website creation-PHP Tutorial

Source: Internet
Author: User
Tags php website
How to use PHP to customize error handling functions for website creation. We all know about the website construction language PHP. because PHP website construction is everywhere, today we are going to talk about how to use PHP to customize the website construction language with incorrect descriptions in Jinan website production www.jnwebseo.com, everyone knows about PHP, because PHP website construction is everywhere. today we Jinan website productionHttp://www.jnwebseo.com to talk about how to use PHP custom error handler, the following code directly.

Function myErrorHandler ($ errno, $ errstr, $ errfile, $ errline ){
If (! (Error_reporting () & $ errno) {return ;}
Switch ($ errno ){
Case E_USER_ERROR:
Echo"My ERROR[$ Errno] $ errstr
";
Echo "error line: $ errline in File: $ errfile
";
Echo "PHP version:". PHP_VERSION. "(". PHP_ OS .")
";
Break;
Case E_USER_WARNING:
Echo"My WARNING[$ Errno] $ errstr
";
Break;
Case E_USER_NOTICE:
Echo"My NOTICE[$ Errno] $ errstr
";
Break;
Default:
Echo "Unknown error type: [$ errno] $ errstr
";
Break;
}
Return true;
}

Function trigger_test ($ age) {// throw an error in the test function
If ($ age <= 0 | $ age> 999) trigger_error ("invalid age: $ age", E_USER_ERROR );
If ($ age <18) trigger_error ("minor: $ age", E_USER_WARNING );
If ($ age> 40 & $ age <100) trigger_error ("slightly older: $ age", E_USER_NOTICE );
}
// If the error is handled in a simple and unified manner:
$ ErrorHandler = set_error_handler ("myErrorHandler ");
Trigger_test (1000); // An error-level error is thrown.


Function myError ($ errno, $ errstr, $ errfile, $ errline ){
Print_r (func_get_args ());
// Specific handling method
}
Function myWarning ($ errno, $ errstr, $ errfile, $ errline ){
Print_r (func_get_args ());
// Specific handling method
}

Function myNtice ($ errno, $ errstr, $ errfile, $ errline ){
Print_r (func_get_args ());
// Specific handling method
}

// If you want to handle different error levels separately:
Set_error_handler ('myerror', E_USER_ERROR );
Set_exception_handler ('mywarning', E_USER_WARNING );
Set_exception_handler ('myntice ', E_USER_NOTICE );
Trigger_error ('intentionally throw an error, which one is very serious! ', E_USER_ERROR );

Ji 'Nan website production http://www.jnwebseo.com to talk about how to use PHP custom errors...

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.