🐡

PHPスクリプトをVScodeでデバッグ実行する(macOS)

2024/09/25に公開

PHPとXdebugのインストール

brew install php
pecl install xdebug

❯ php -i  | grep xdebug. | head
Support Xdebug on Patreon, GitHub, or as a business: https://xdebug.org/support
             Enabled Features (through 'xdebug.mode' setting)             
'xdebug://gateway' pseudo-host support => no
'xdebug://nameserver' pseudo-host support => no
xdebug.auto_trace => (setting renamed in Xdebug 3) => (setting renamed in Xdebug 3)
xdebug.cli_color => 0 => 0
xdebug.client_discovery_header => HTTP_X_FORWARDED_FOR,REMOTE_ADDR => HTTP_X_FORWARDED_FOR,REMOTE_ADDR
xdebug.client_host => localhost => localhost
xdebug.client_port => 9003 => 9003
xdebug.cloud_id => no value => no value

VScodeのXdebug拡張のインストール

https://marketplace.visualstudio.com/items?itemName=xdebug.php-debug

プロジェクトの作成

mkdir php-app
cd php-app
touch index.php
code index.php
index.php
<?php

$greet = 'Hello World';
echo $greet;

launch.jsonの作成

launch.json
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug current script in console",
            "type": "php",
            "request": "launch",
            "program": "${file}",
            "cwd": "${fileDirname}",
            "externalConsole": false,
            "port": 9003,
            "runtimeArgs": [
                "-dxdebug.mode=debug",
            ],
        }
    ]
}

デバッグの実行

  1. VScodeの左メニューの虫眼鏡マークをクリック(Ctrl+Shift+D)
  2. index.phpを選択
  3. echo $greet;の行にブレークポイントを設定
  4. F5キーを押す
  5. $greetの値を確認

Discussion