Search code examples
visual-studio-codexdebugphp-7.2xdebug-3

How to create launch.json file and start debugging PHP in VSCode?


I installed PHP debug extension in VSCode, installed Xdebugger also by analyzing phpinfo() file. Now I am unable to debug the file (forward arrows are not coming for debugging).

How to write launch.json file and start debugging?

settings.json

{
    "launch": {
        "configurations": [
            {
                "name": "Launch currently open script with Xdebug 2 (Legacy)",
                "type": "php",
                "request": "launch",
                "program": "${file}",
                "cwd": "${fileDirname}",
                "port": 0,
                "runtimeArgs": [
                    "-dxdebug.remote_enable=yes",
                    "-dxdebug.remote_autostart=yes"
                ],
                "env": {
                    "XDEBUG_CONFIG": "remote_port=${port}"
                }
            },
            {
                "name": "Listen for Xdebug",
                "type": "php",
                "request": "launch",
                "port": 9003
            }
        ]
    }
}

Solution

  • There are many information missing from this question. Firstly you should make sure that php-xdebug module has been correctly installed, it's indeed safe to bet that if xdebug module appears in phpinfo() it's correctly installed but it's not always the case. I used to face the same problem where the module appeared there but there was an error when I was checking my php version (php -v). You should also make sure that the json configuration file you provided is configured based on your xdebug.ini file. For example if your xdebug.ini file looks like this:

    [xdebug]
    zend_extension=/usr/lib/php/20190902/xdebug.so
    xdebug.mode = debug
    xdebug.start_with_request = yes
    xdebug.client_port = 9000
    

    Then your json configuration file should look like this:

    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "Listen for XDebug",
                "type": "php",
                "request": "launch",
                "port": 9000,
            },
            {
                "name": "Launch currently open script",
                "type": "php",
                "request": "launch",
                "program": "${file}",
                "cwd": "${fileDirname}",
                "port": 9000
            }
        ]
    }
    
    

    I strongly suggest you to install xdebug from it's wizard where you can simply paste your phpinfo() and get the correct installation process.