萬盛學電腦網

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

PhpStorm + Xdebug 斷點除錯完整配置圖文教學

前言

PhpStorm 是 PHP 開發最強大的 IDE,配合 Xdebug 可實現斷點、單步執行、變數檢查、堆疊追蹤等專業除錯體驗。本文記錄 PHP 5.6 + PhpStorm 8.x 完整配置流程(新版大同小異)。

環境版本

  • PhpStorm 8.0.1+
  • PHP 5.6.2 (VC11 x64 NTS)
  • Xdebug 2.2.5 (php_xdebug-2.2.5-5.6-vc11-x86_64.dll)
  • 關鍵:PHP 版本、VC 編譯版本、TS/NTS、位元數必須與 Xdebug 完全一致

1. PHP 安裝 Xdebug 擴展

編輯 php.ini:

[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

重啟 Apache/Nginx,查看 phpinfo() 出現 Xdebug 區塊即成功。
phpinfo Xdebug

或使用 phpStudy:其他選項 → PHP擴展設置 → XDEBUG 勾選即可。

2. PhpStorm 設定

2.1 Debug Port 設定

File → Settings → Languages & Frameworks → PHP → Debug
Debug port = 9000(與 php.ini 一致)
Debug port Xdebug 設定

2.2 Server 設定

File → Settings → Languages & Frameworks → PHP → Servers
Name: localhost, Host: localhost, Port: 80, Debugger: Xdebug
Server 設定

2.3 DBGp Proxy 設定

File → Settings → Languages & Frameworks → PHP → Debug → DBGp Proxy
IDE key: PHPSTORM, Host: localhost, Port: 80

2.4 Debug Configuration

Run → Edit Configurations → + → PHP Web Application
Server: 選擇剛建立的 localhost
Start URL: https://www.51open.pcom(你的本地域名)
Browser: 選擇 Chrome/Firefox
Debug Config Add Server Server Detail Browser OK

3. 開始除錯

  1. 編輯器行號處點擊設定斷點(紅點)。
  2. 點擊工具列 甲蟲圖示 (Debug) 或 Shift + F9 啟動。
  3. 瀏覽器自動開啟 Start URL,程式執行至斷點暫停,PhpStorm 切換到 Debug 面板顯示變數、堆疊、Watches。
    Debug 運行

常見問題排查

  • 斷點不生效:檢查 xdebug.remote_connect_back=1、防火牆 9000 埠、IDE Key 一致。
  • 路徑映射錯誤:Server 設定中配置 Path mappings(本地專案路徑 ↔ 伺服器絕對路徑)。
  • 多站點:每個虛擬主機建立獨立 Server 設定。

新版 PhpStorm (2023+) 簡化流程

  1. Run → Start Listening for PHP Debug Connections(電話圖示變綠)。
  2. 瀏覽器安裝 Xdebug Helper 擴充套件,設定 IDE Key = PHPSTORM,點擊 Debug 圖示。
  3. 重整頁面,PhpStorm 自動中斷於斷點。
copyright © 萬盛學電腦網 all rights reserved