1. 程式人生 > 實用技巧 >VSCode 配置 Php 環境

VSCode 配置 Php 環境

版本:PHP 7.4.9

安裝步驟

  1. 下載 php 安裝包和 xdebug 的 dll
    將 xdebug 的 dll 放在 php 目錄的 ext 資料夾中,並將 php.ini-development 作為 php.ini,在其中新增對 xdebug 的配置:

    [xdebug]
    zend_extension="C:/Program Files/php-7.4.9-Win32-vc15-x64/ext/php_xdebug-2.9.6-7.4-vc15-x86_64.dll"
    xdebug.remote_enable=1
    xdebug.remote_autostart=1
    #與remote_connect_back不能同時開啟
    xdebug.remote_host="localhost"
    xdebug.remote_port=9000
    #與remote_host不能同時開啟
    #xdebug.remote_connect_back = 1
    xdebug.remote_handler="dbgp"
    
  2. 配置環境變數
    將 php 解壓之後的檔案路徑放在 Path 的最後

  3. VSCode 安裝兩個外掛:PHP Debug 和 PHP IntelliSense

  4. 新增/修改 VSCode 中 php 相關的設定

    這個選項要勾掉,也即禁用 VSCode 自帶的語法提示

    "php.validate.executablePath": "C:\\Program Files\\php-7.4.9-Win32-vc15-x64\\php.exe",
     // For IntelliSense
     "php.executablePath": "C:\\Program Files\\php-7.4.9-Win32-vc15-x64\\php.exe",
    

    配置 php 可執行檔案的路徑

    // === php formatter start ===
     //列印日誌資訊,用於除錯
     "phpformatter.logging": true,
     //不使用composer方式
     "phpformatter.composer": false,
     //新增自定義引數,預設的引數level已經在新版本中移出所以會導致執行出錯
     //RULES=[@PSR1,@PSR2,@Symfony]
     //source:https://github.com/FriendsOfPHP/PHP-CS-Fixer#usage
     "phpformatter.arguments": [
         "--rules=@Symfony"
     ],
     // Should point to php-cs-fixer.phar file, if you have installed this manually (without Composer). Should include .phar extension.
     // php-cs-fixer.phar路徑,使用composer方式時可以不填
     "phpformatter.pharPath": "C:/Program Files/php-7.4.9-Win32-vc15-x64/ext/php-cs-fixer-v2.phar",
     // If the pharPath is set, and you are not using Composer, and you haven't added PHP to your PATH, this should point to the php.exe file.
     // php路徑,使用composer方式時可以不填
     "phpformatter.phpPath": "C:\\Program Files\\php-7.4.9-Win32-vc15-x64\\php.exe",
     "php.suggest.basic": false
     // === php formatter end ===
    

    配置 php formatter(需提前下載 php-cs-fixer-v2.phar)

  5. 斷點除錯
    需要以開啟資料夾的形式才能成功設定斷點除錯,對單個檔案無效

    launch.json 中的埠必須要和在 php.ini 中 配置 xdebug 時指定的埠一致:

    xdebug.remote_port=9000
    

參考: