search
Homephp教程php手册通过浏览器的Console进行PHP调试

通过浏览器的Console进行PHP调试

Jun 06, 2016 pm 08:14 PM
phpconsoleBrowser

当我们的PHP程序Debug时,比较常用的方式是将变量的信息打印到浏览器中,例如这样:

    <?php  
    echo &#39;<pre class="brush:php;toolbar:false">';  
    print_r($bar);  
    echo '
';       exit;

可是直接打印出来有可能会干扰你的页面,打乱布局,影响作为Api返回的数据等问题。这时我们可以采取将调试信息输出到文件的方式,如下是最简单的例子:

error_log(print_r($bar, true));

可是这种方式用来记录日志还合适,用到调试时终究还是不太方便。此时我们可以利用现代浏览器的控制台,比如Chrome Console(Win快捷键 Ctrl+Shift+J),将内容输出到Console里,就可以解决这个问题了。

现在已经有一些这样的工具了,比如说 PHP-Console 或者Chrome Logger ,前者针对PHP,后者支持多种服务端语言,使用方式都是先安装一个Chrome扩展,然后提供服务端的库供调用。实现方式大致是利用Session,Cookies等来传递数据。

如上是比较完善的解决方式,都提供了丰富的功能。但如果只是想要一个简单的,可用的,不用安装扩展程序的方式来解决这个问题,那么可以这么来做:

    <?php  
    function console_log($data)  
    {  
        if (is_array($data) || is_object($data))  
        {  
            echo("<script>console.log('".json_encode($data)."');</script>");  
        }  
        else  
        {  
            echo("<script>console.log('".$data."');</script>");  
        }  
    }

其实质上就是在页面里加入了一段javascript的脚本,利用 console.log();函数输出信息到控制台,如上方法打印出来都是字符串形式,如果将单引号去掉,PHP的数组和对象将以JS对象的方式打印出来,即

echo("<script>console.log(".json_encode($data).");</script>");

究竟使用哪一种看你喜欢的方式了。

如上就是最简单的实现方式了,但这么做有一点不太好,就是输出的很凌乱,如果你在不同的位置都调用了该函数,那么在页面的各个位置都将插入一段

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.