清风听雨 发表于 2017-4-3 13:05:29

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

作者:Haohappy
MSN: haohappy at msn.com
Blog: http://blog.csdn.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
require_once(‘abc.php’);
?>

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

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

testXdebug2.php

<?php
testXdebug();
function testXdebug() {
require_once('abc.php');
}
?>

输出信息:

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

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

testXdebug3.php

<?php
testXdebug();
function testXdebug() {
requireFile();
}
function requireFile() {
require_once('abc.php');
}
?>
输出信息:

呵呵,也就是说Xdebug具有类似于Java的Exception的“跟踪回溯”的功能,可以根据程序的执行一步步跟踪到出错的具体位置,哪怕程序中的调用很复杂,我们也可以通过这个功能来理清代码关系,迅速定位,快速排错。
页: [1]
查看完整版本: 使用Xdebug调试和优化PHP程序[2]