display_errors = Off error_reporting = E_ALL
display_errors = On
error_reporting = E_ALL & ~E_NOTICE
php_flag display_errors on php_value error_reporting 2039
<?php print("The next line generates an error.<br>"); printaline("PLEASE?"); print("This will not be displayed due to the above error."); ?>
<?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>
... foreach($_GET as $key => $i){ print("Correct data? " . $_GET[$key] . "<br>"); print("$key=$j<br>"); } ...
放進去的 print 語句是粗體。注意,現在已經知道在 Web 瀏覽器上顯示的 $key 值是正確的,但是由于某些原因,值沒有正確地顯示。請看新的輸出,如圖 3 所示。
圖 3. 修改后的 test2.php 的輸出
現在已經知道應用程序正確地從 GET 請求接收到了變量,那么肯定是在代碼中有 bug。查看之后注意到,用來顯示值的變量 $j 是錯誤的。在 foreach 語句中指定的是 $i,所以它肯定會有正確的值,但是無意之中輸入了 $j。所以通過把 $j 替換成 $i,迅速地修正了錯誤,重新載入頁面之后,就看到了正確的輸出,如圖 4 所示。
圖 4. 修正后的 test2.php 的輸出
現在可以刪除或注釋掉剛才添加的 print 語句了,因為已經發現了代碼中的 bug。注意,這只是在調試應用程序時可能遇到的許多錯誤中的一個很小的子集。對于使用數據庫時可能遇到的問題,一個好的解決方案是輸出 SQL 語句,以確保執行的 SQL 就是想要執行的。
現在要來看看如何使用 Eclipse IDE 和 PHPEclipse 插件及調試器擴展進一步在調試歷程中提供幫助。
新聞熱點
疑難解答