2

enter image description here There is this play button in VSCode that allows to either Run PHP File or Debug PHP File. I'm wondering how I can configure that PHP is started with additional command line arguments, as I am debugging a CLI script. I can install the PHP Debug extension and create a launch.json file. When I add an args member to a certain section, I can run the command line using a different play button.

There is this question, here on Stackoverflow, that says the Python debug extension accepts a purpose member in the launch file to make the debugger consider command line arguments.

How can I use said button in PHP CLI development?

1
  • php uses xdebug for debugging. it has to be installed on the server and configured in vs code Commented Nov 12, 2023 at 1:10

2 Answers 2

2

you need to install php-xdebug on your system, the following document explains how to install it:

start your vscode, make sure you have installed the xdebug extension for vscode then create a file in .vscode/launch.json, copy & paste this:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for Xdebug",
            "type": "php",
            "request": "launch",
            "port": 9000
        },
        {
            "name": "PHP CLI some_file.php",
            "type": "php",
            "request": "launch",
            "cwd": "${workspaceFolder}",
            "program": "${workspaceFolder}/some_file.php",
            "args": [
                "hello",
                "world"
            ],
            "env": {
                "XDEBUG_MODE": "debug",
                "XDEBUG_TRIGGER": "1",
                "XDEBUG_CONFIG": "client_port=${port}"
            }
        },
        {
            "name": "PHP CLI EXTERNAL CONSOLE some_file.php",
            "type": "php",
            "request": "launch",
            "externalConsole": true,
            "cwd": "${workspaceFolder}",
            "program": "${workspaceFolder}/some_file.php",
            "args": [
                "hello",
                "world"
            ],
            "env": {
                "XDEBUG_MODE": "debug",
                "XDEBUG_TRIGGER": "1",
                "XDEBUG_CONFIG": "client_port=${port}"
            }
        }
    ]
}

now for example, create a some_file.php, paste this:

<?php
    var_dump($argv);
?>

you can now debug the cli by pressing the play button like you mentioned above, select "PHP CLI some_file.php" or "PHP CLI EXTERNAL CONSOLE some_file.php" in the drop down, and press the green play button or press F5, mark the breakpoint at line 2 or edit some_file.php and mark it wherever you want, that's it.

pay attention to the differences between the two, the external one has the following parameters: externalConsole": true,

and if you want to trigger the debug from your terminal to vscode, select "Listen for Xdebug" from the drop down and press the green play button or press F5, now from your terminal, type this:

XDEBUG_MODE=debug;XDEBUG_TRIGGER=1 php some_file.php hello world
Sign up to request clarification or add additional context in comments.

Comments

0

You kinda answered your own question, use the launch.json file to configure the command which is executed when you press the "run and debug" button, the options are extension specific in you case PHP Debug description have list of supported options and there is section "Options specific to CLI debugging", you either look for args (script args) or runtimeArgs (php binary args) option.

The purpose option is not available for PHP. For python you have another "run" button, it mimics the "run and debug" button but there was/is bug which ignored the config for "run" button, which the purpose config option solve, but restarting VSCode solve it too.

You didn't said specifically what your issue is, looks like you provide args and they are not observed somewhere, you might have issue down the line. Provide more info like your launch.json config and output of debug console.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.