Web authoring How to customize error handling functions with PHP _php tutorial

Source: Internet
Author: User
Tags php website
Say the website building language, we all know PHP, because PHP website construction has been everywhere, today we Jinan website ProductionHttp://www.jnwebseo.com to talk about how to use PHP to customize the error handling function, the bottom of the direct code.

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) {//The test function that throws the error
if ($age <= 0 | | $age > 999) trigger_error ("Age not valid: $age years old", E_user_error);
if ($age < Trigger_error) ("Minors: $age Years", e_user_warning);
if ($age > && $age < Trigger_error) ("Slightly older: $age years old", E_user_notice);
}
If the error is handled simply and uniformly:
$errorHandler = Set_error_handler ("MyErrorHandler");
Trigger_test (1000);//throws an error level


function Myerror ($errno, $errstr, $errfile, $errline) {
Print_r (Func_get_args ());
Specific Treatment methods
}
function mywarning ($errno, $errstr, $errfile, $errline) {
Print_r (Func_get_args ());
Specific Treatment methods
}

function Myntice ($errno, $errstr, $errfile, $errline) {
Print_r (Func_get_args ());
Specific Treatment methods
}

If you want to handle different levels of errors individually:
Set_error_handler (' Myerror ', e_user_error);
Set_exception_handler (' mywarning ', e_user_warning);
Set_exception_handler (' Myntice ', e_user_notice);
Trigger_error (' intentionally throwing a mistake, or a very serious one! ', e_user_error);

http://www.bkjia.com/PHPjc/477306.html www.bkjia.com true http://www.bkjia.com/PHPjc/477306.html techarticle say the website building language, everybody knows PHP, because PHP website construction already everywhere, today our Jinan website production http://www.jnwebseo.com to talk about is how to use PHP custom 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.