利用浏览器控制台调试PHP数据

/**
 * 利用浏览器控制台调试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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容