/**
* 利用浏览器控制台调试PHP数据
*
* @param [type] $mixed 任意数据类型
* @param string $color 颜色值,HEX或者英文字符
* @param string $flag 一个简单的标记,为自己做的面包屑
* @return string
*/
function console($mixed, $color = 'black', $flag = 'debug') {
static $num = 0;
// 累计调用次数
$num++;
// 获取类型
$type = ucfirst(gettype($mixed));
// 调试数据
$json = json_encode($mixed);
// 跟踪信息
$trace = debug_backtrace()[0];
// 跟踪文件位置
$file = str_replace('\\', '/', $trace['file']);
// 跟踪行号
$line = $trace['line'];
$fn = "console.log('%c{$num}.{$flag} ', 'color:{$color}', 'File: {$file},', 'Line:{$line},', 'Type:{$type},', 'Value:', {$json})";
echo '<script>'. $fn .'</script>';
}
- 开始使用
$a = 100;
console($a);
$b = 'hello';
console($b, 'red');
$c = true;
console($b, 'green', 'whoami:');
- 查看浏览器控制台
Alt text