PHP prints call stack information

Source: Internet
Author: User
PHP prints the call stack information. an error occurs in PHP. We use set_error_handler for processing. if an exception occurs, use set_exception_handler. However, in debugging, we can also use

Debug_print_backtrace and debug_backtrace print the call stack information

Use of set_error_handler

 My ERROR [$errno] $errstr
\n"; echo " Fatal error on line $errline in file $errfile"; echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")
\n"; echo "Aborting...
\n"; exit(1); break; case E_USER_WARNING: echo "My WARNING [$errno] $errstr
\n"; break; case E_USER_NOTICE: echo "My NOTICE [$errno] $errstr
\n"; break; default: echo "Unknown error type: [$errno] $errstr
\n"; break; } /* Don't execute PHP internal error handler */ return true;}// function to test the error handlingfunction scale_by_log($vect, $scale){ if (!is_numeric($scale) || $scale <= 0) { trigger_error("log(x) for x <= 0 is undefined, you used: scale = $scale", E_USER_ERROR); } if (!is_array($vect)) { trigger_error("Incorrect input vector, array of values expected", E_USER_WARNING); return null; } $temp = array(); foreach($vect as $pos => $value) { if (!is_numeric($value)) { trigger_error("Value at position $pos is not a number, using 0 (zero)", E_USER_NOTICE); $value = 0; } $temp[$pos] = log($scale) * $value; } return $temp;}$old_error_handler = set_error_handler("myErrorHandler");?>

Use of set_exception_handler

 GetTrace (); foreach ($ trace as $ key => $ stackPoint) {// The Returned exception is similar, exception description $ trace [$ key] ['args '] = array_map ('gettype', $ trace [$ key] ['args']);} // format the exception information $ result = array (); foreach ($ trace as $ key => $ stackPoint) {$ result [] = sprintf ($ traceline, $ key, $ stackPoint ['file'], $ stackPoint ['line'], $ stackPoint ['function'], implode (',', $ stackPoint ['args']);} // trace always ends with {main} $ result [] = '#'. ++ $ Key. '{main}'; // write tracelines into main template $ msg = sprintf ($ msg, get_class ($ exception), $ exception-> getMessage (), $ exception-> getFile (), $ exception-> getLine (), implode ("\ n", $ result), $ exception-> getFile (), $ exception-> getLine (); error_log ($ msg) ;}?>

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.