萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> PHP 程式設計

PhpStorm 調試 PHP 完整教程:Xdebug 配置與除錯實戰

前言

本文為 PhpStorm 8.x + PHP 5.6 + Xdebug 2.2.5 環境下的詳細調試配置指南,新版操作邏輯一致,介面略有差異。

1. PHP 安裝 Xdebug

[xdebug]
zend_extension="D:\wamp\php-5.6.2-x64\ext\php_xdebug-2.2.5-5.6-vc11-x86_64.dll"
xdebug.remote_enable = On
xdebug.remote_handler = dbgp
xdebug.remote_host = localhost
xdebug.remote_port = 9000
xdebug.idekey = PHPSTORM

驗證:phpinfo() 出現 Xdebug 區塊。
phpinfo Xdebug

2. PhpStorm 配置

2.1 Xdebug Port 對齊

Settings → Languages & Frameworks → PHP → Debug,Port = 9000。
Debug Port Xdebug Settings

2.2 Debug Configuration

Run → Edit Configurations → + → PHP Web Application
Name: 專案名,Server: 新增/選擇 localhost:80 Xdebug,Start URL: 你的本地站點。
Edit Config Add Server

2.3 Server 路徑映射

在 Server 設定中勾選 Use path mappings,將本地專案目錄對應到伺服器 DocumentRoot。
Path Mapping Mapping Detail OK

3. 開始調試

  1. 程式碼行號左側點擊設定斷點。
  2. 點擊工具列 甲蟲圖示 (Debug) 或 Shift+F9。
  3. 瀏覽器開啟頁面,PhpStorm 於斷點暫停,顯示 Frames、Variables、Watches。
    Debug Running

調試操作技巧

  • Step Over (F8):單步跳過函式呼叫
  • Step Into (F7):進入函式內部
  • Run to Cursor (Alt+F9):直接執行至游標行(極佳)
  • Evaluate Expression (Alt+F8):即時運算任意表達式
  • 條件斷點:右鍵斷點 → Condition 輸入表達式(如 $i > 100)

停止調試

再次點擊甲蟲圖示(變紅)或 Ctrl+F2,關閉 Debug 面板回到編輯視圖。

總結

掌握 PhpStorm + Xdebug 除錯流程,能大幅縮短定位 Bug 時間,是 PHP 開發者必備技能。

copyright © 萬盛學電腦網 all rights reserved