Home > php教程 > php手册 > body text

一个PHP简单的异常处理代码

WBOY
Release: 2016-06-06 19:38:00
Original
1471 people have browsed it

无详细内容 无 ?php/** * 异常处理 * @return void */function debug_user_handler($errno, $errstr, $errfile, $errline, $errcontext){ob_start();debug_print_backtrace();$trace = ob_get_contents();ob_end_clean();$data = array(isset($_SERVER['REQUE

<?php

/**
 * 异常处理
 * @return void
 */
function debug_user_handler($errno, $errstr, $errfile, $errline, $errcontext)
{
	ob_start();
	debug_print_backtrace();
	$trace = ob_get_contents();
	ob_end_clean();

	$data = array(
			isset($_SERVER[&#39;REQUEST_URI&#39;]) ? $_SERVER[&#39;REQUEST_URI&#39;] : "",
			date("Y-m-d H:i:s"),
			"错误号{$errno}, 文件:{$errfile} 行号: {$errline}",
			$errstr,
			$trace);	
	trigger_error(implode("\n", $data) . &#39;(&#39; . $errno . &#39;)&#39;);
}
// 可以设置不同异常等级
set_error_handler(&#39;debug_user_handler&#39;, E_USER_NOTICE|E_USER_WARNING);

// 测试
function test_handler()
{
	$x = mt_rand(0, 100);
	echo "x=$x\n";
	if ($x > 50) {
		trigger_error("x great than 50", E_USER_NOTICE);	
	} else {
		trigger_error("x less than 50", E_USER_WARNING);	
	}
}

test_handler();
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!