Is Visual Studio Code's PHP debug console interactive?

Viewed 4373

I am using Visual Studio Code 1.16.1 together with Felix Becker's PHP Debug extension. I'm connecting to XDebug just fine, can set breakpoints and view variables in the debug pane without any issues.

However, the debug console seems kind of useless, I can only run super basic PHP commands, and I can't seem to evaluate normal PHP commands or interact with my app very well.

I consistently get error evaluating code when attempting to type any PHP statements or expressions in the debug console. It seems like all I am able to do is declare variables, arrays, and objects.

I can't declare classes, functions, use control structures (if, foreach, etc.).

Works:

$x = 4
//4

$x
//4

$x = new stdClass();
//stdClass

$x = [];
//array(0)

($x) ? yes : no
// yes

(!$x) ? yes : no
// no

preg_replace('/dog/', 'cat', 'The quick brown fox jumps over the lazy dog.')
// "The quick brown fox jumps over the lazy cat."

request()
//Illuminate\Http\Request (Laravel helper methods work)

Doesn't Work:

echo "yes"
//error evaluating code

if ($x == 4) { echo "yes" }
//error evaluating code

for ($i=0; $i < 5; $i++) { }
//error evaluating code

function foo() {}
//error evaluating code

class SimpleClass {}
//error evaluating code

$var_dump($x)
//null

Is the debug console supposed to act like a true REPL? I know PHPStorm's console can evaluate any PHP you throw at it, can Visual Studio Code do the same? Is anyone else facing this problem?

Thanks.

enter image description here

2 Answers

TL;DR; It's an XDebug related issue, not related to OP's extension, and canceling the watch variables may fix XDebug behaviour.


The issue is not related to OP's extension, it's an XDebug related issue.

Many people workaround this issue, by removing the variables from VSCode's "Watch" list, in the debug view.

In following screenshot, the red arrow indicates a button where you can delete all watch-expressions with once click:

VSCode's watch variable section

See also: github.com/xdebug/vscode-php-debug/issues/192

You are limited to what is actual debugging. It' useful to interact with an active program to fix it, not as a playground like in a Python or a Javascript console. I you want to create some scripts, create a PHP CLI file.

You should use print instead of echo and var_dump() not $var_dump(). Then it depends if you are in CLI mode or in FPM mode (web-server). If you are connected to a web server in a debugging session, it will not print back into the debugger terminal but into the web server response.

Related