使用Xdebug调试和优化PHP程序[2]

<iframe align="top" marginwidth="0" marginheight="0" src="http://www.zealware.com/46860.html" frameborder="0" width="468" scrolling="no" height="60"></iframe>

作者:Haohappy

MSN: haohappy at msn.com

Blog: http://blog.****.net/haohappy2004

<chsdate year="2006" month="7" day="4" islunardate="False" isrocdate="False" w:st="on"><strong style=""><span lang="EN-US" style="font-size: 7.5pt; color: rgb(255, 102, 0); font-family: Verdana;">2006-07-04</span></strong></chsdate>

Go on..现在我们来从最简单的程序调试开始一步步介绍Xdebug。

调试:

我们先写一个可以导致执行出错的程序,例如尝试包含一个不存在的文件。

testXdebug.php

<?php <o:p>

require_once(‘abc.php’);

?>

然后通过浏览器访问,我们惊奇地发现,出错信息变成了彩色的了:

使用Xdebug调试和优化PHP程序[2]

不过除了样式改变,和我们平时打印的出错信息内容没什么不同,意义不大。好,我们继续改写程序:

testXdebug2.php

<?php <o:p>

testXdebug();

function testXdebug() {

require_once('abc.php');

}

?>

输出信息:

使用Xdebug调试和优化PHP程序[2]

发现了什么? Xdebug跟踪代码的执行,找到了出错的函数testXdebug()

我们把代码再写得复杂一些: 

testXdebug3.php

<?php <o:p>

testXdebug();

function testXdebug() {

requireFile();

}

function requireFile() {

require_once('abc.php');

}

?>

输出信息:

使用Xdebug调试和优化PHP程序[2]

呵呵,也就是说Xdebug具有类似于JavaException的“跟踪回溯”的功能,可以根据程序的执行一步步跟踪到出错的具体位置,哪怕程序中的调用很复杂,我们也可以通过这个功能来理清代码关系,迅速定位,快速排错。



Trackback: http://tb.blog.****.net/TrackBack.aspx?PostId=893060