PHP使用xdebug與netbeans進行除錯
本範例安裝xampp為範例,xampp版本為v3.2.2,Apache/2.4.25 (Win32) OpenSSL/1.0.2j PHP/7.1.4,netbeans版本為8.2。
php新增xdebug功能
Step1)下載Xdebug,到https://xdebug.org/download.php下載PHP 7.1 VC14 TS (32 bit)。
Step2)將下載的檔案php_xdebug-2.5.4-7.1-vc14.dll 拷貝到C:\xampp\php\ext下,重新命名為php_xdebug.dll
Step3)編輯C:\xampp\php\php.ini
點選XAMPP的Apache->config->PHP(php.ini),編輯php.ini
將output_buffering改成Off
output_buffering = Off
於php.ini最後新增以下內容。
[XDebug]
zend_extension = "c:\xampp\php\ext\php_xdebug.dll"
xdebug.remote_autostart = 1
xdebug.remote_enable = 1
xdebug.remote_handler = "dbgp"
xdebug.remote_host = "127.0.0.1"
xdebug.remote_log="c:\xampp\tmp\xdebug.txt"
xdebug.remote_port = 9000
xdebug.trace_output_dir = "c:\xampp\tmp"
xdebug.remote_cookie_expire_time = 36000
Step4)重新啟動Apache
點選XAMPP的Apache->stop
接著點選XAMPP的Apache->start
Step5)瀏覽xampp中的phpinfo看看有沒有多出xdebug,若看到此畫面表示xdebug安裝完成。
netbeans啟用xdebug
Step1)在netbeans新增專案,File->New Project。
選擇PHP->PHP application with Existing Sources,點選Next
選取「Sources Folder」,「PHP Versions」,點選Next
Run As 本範例選擇「Local Web Site」,「Project URL」有時需要加上資料夾名稱,網址才正確,最後點選Finish。
Step2)開啟檔案,點選Source Files內的檔案
Step3)點選左側的行號設定中斷點,本範例在第6行設定中斷點
Step4)點選檔案後,按下滑鼠右鍵,選擇「Debug」進行除錯
Step5)netbeans會自動開啟瀏覽器,apache啟用port9000,netbeans會連線port9000,進行php除錯。
Step6)在netbeans內點選「」進行除錯,會看到程式一行一行執行,下方Variables視窗可以看到過程中變數的值。