首页 > 技术文章 > dump调试函数

wish123 2017-04-14 11:59 原文

//dump调试函数
if (!function_exists('dump')) {
/*
* dump调试函数
*/
function dump($var)
{
$traces = debug_backtrace();
foreach ($traces as $trace) {
if (isset($trace['function']) && in_array($trace['function'], array('dump'))) {
echo '<small>' . $trace['file'] . ':' . $trace['line'] . '</small>' . "\n";
break;
}
}
array_map(function ($var) {
ob_start();
var_dump($var);
$output = ob_get_clean();
if (!extension_loaded('xdebug')) {
$output = preg_replace('/\]\=\>\n(\s+)/m', '] => ', $output);
echo '<pre>' . htmlspecialchars($output, ENT_QUOTES) . '</pre>';
} else {
$traces = debug_backtrace();
foreach ($traces as $trace) {
if (isset($trace['function']) && in_array($trace['function'], array('dump'))) {
echo preg_replace('/<small>[^>]*:\d+:<\/small>\r?\n?/i', '<small style="color:#999;">→</small> ', $output);
break;
}
}
}
}, func_get_args());
}
}

调用:
dump(11,22,[3,4],["a"=>5,"b"=>6], new \stdClass());
输出:

 

推荐阅读