search
Homephp教程php手册如何优雅的输出PHP调试信息

如何优雅的输出PHP调试信息

Jun 06, 2016 pm 07:44 PM
phpgraceinformationAppearhowdebugoutput

经常因为出现紧急bug而被老板骂的同事,为了更快的修复而直接利用线上的错误环境现场debug,并直接在页面上echo和dump。结果被老板发现了,又是一通臭骂。那么有没有什么办法更优雅的输出PHP调试信息呢? 这里提供一个方案:利用浏览器调试模式的Console 1,

经常因为出现紧急bug而被老板骂的同事,为了更快的修复而直接利用线上的错误环境现场debug,并直接在页面上echo和dump。结果被老板发现了,又是一通臭骂。那么有没有什么办法更优雅的输出PHP调试信息呢?

这里提供一个方案:利用浏览器调试模式的Console

1,先定义一个php函数:

<span> 1</span> <span>function</span> console(<span>$log</span>=''<span>)
</span><span> 2</span> <span>{
</span><span> 3</span>     <span>switch</span> (<span>empty</span>(<span>$log</span><span>)) {
</span><span> 4</span>         <span>case</span> <span>False</span>:
<span> 5</span>             <span>$out</span> = json_encode(<span>$log</span><span>);
</span><span> 6</span>             <span>$GLOBALS</span>['console'] .= 'console.log('.<span>$out</span>.');'<span>;
</span><span> 7</span>             <span>break</span><span>;
</span><span> 8</span>         
<span> 9</span>         <span>default</span>:
<span>10</span>             <span>echo</span> '<script type="text/javascript">'.<span>$GLOBALS['console'].'</script>'<span>;
</span><span>11</span> <span>    }
</span><span>12</span> }

 

 

2,在需要调试的地方直接调用它,递交的参数可以是字符串,也可以是数组。
如何优雅的输出PHP调试信息

 

3,在适当的位置调用无参数的console,用来输出调试信息,比如各类php框架入口文件的最底部
如何优雅的输出PHP调试信息

 

4,打开浏览器(比如chrome),载入页面后进入调试模式(比如按F12键),点击Console,你会发现所有数据都在这里等着你查阅。
如何优雅的输出PHP调试信息

 

5,其他用途
优秀的PHP开源框架ThinkPHP提供了输出运行状态的功能,你可以通过运行状态来了解某个页面在后台处理了多长时间。但是,很多人都注意到了,它是不准确的。

下面我们试着用刚刚加进来的自定义函数console在浏览器中输出真正的运行时间。
所有工作都在入口文件中完成:
如何优雅的输出PHP调试信息

在所有代码的最开头,定义变量$s=microtime(true);
在console();的上面再增加console('exetime:'.round(microtime(true)-$s,4));

好了,我们来看看真正的运行时间吧:
如何优雅的输出PHP调试信息

 

这个课题就这么简单愉快的解决了。
PS:因为这个函数用到了json来输出数组,所以要求编码为UTF-8,如果使用其它编码可能会出现未知问题,需要留意。

 

Statement
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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

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

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),

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool