<?php
$j = "";
print("Lets retrieve all the variables submitted to this ");
print("script via a GET request:<br>");
foreach($_GET as $key => $i){
print("$key=$j<br>");
}
if($_GET['Submit'] == "Send GET Request")
$j = "done!<br>";
?>
<form method="GET">
Name: <input name="name"><br>
Email: <input name="email" size="25"><br>
<input name="Submit" type="submit" value="Send GET Request">
</form>
您可能会非常容易地发现清单 2 中的 bug!您很棒!但请注意这是一个非常简单的脚本,只是作为使用 print 语句进行调试而展示的一个例子而已。这个脚本只是提取 GET 请求中的所有变量,如果有,就把它们显示在浏览器上。还提供了一个表单,用 GET 请求向服务器发送变量以进行测试。请看输出,如图 2 所示。
对于本文中使用的插件和扩展的版本,断点功能是必需的,因为 PHP 在把输出发送到浏览器之前会缓冲它。除此之外,需要做的不仅仅是设置一个断点把当前显示数据刷新到 Web 浏览器,所以要像下面和图 8 所示那样定义 test4.php。
清单 4. 设置和创建断点
<?php
function break-point(){
ob_flush();
flush();
sleep(.1);
debugBreak();
}
print("This will get shown first, ");
print("as will this<br>");
breakpoint();
print("This won't get shown until after ");
print("continuing the break-point<br>");
breakpoint();
print("END!");
?>
breakpoint() 函数会把缓冲的输出和其他缓冲的数据刷新到 Web 浏览器。对 sleep(.1) 的调用是必需的,这样代码中止于 debugBreak() 之前,服务器才有足够的时间把数据刷新到 Web 浏览器,这个函数是前面下载的 PHP 调试器扩展的内部函数。这样,调用 breakpoint() 会把 HTML 块、print() 和 echo() 语句的数据刷新到浏览器,然后中止代码执行。