Jump to content

Recommended Posts

I try to run my PHP code in VS Code, but get a message in the console telling me: Cannot evaluate code without a connection. I tried to set up a connection to my webhotel MySQL account, but
nothing happens. And why can't I choose "Debug PHP Script" from the context menu to the right for the play/debug button in the upper right corner? Other choices except Run PHP Script are disabled.

Link to comment
https://forums.phpfreaks.com/topic/326898-vs-code-ask-for-a-connection/
Share on other sites

Debugging a web application is a non-trivial exercise in most situations.

VSCode is not a web server, so if you're developing a web application, you are not going to have much luck without a good understanding of both xdebug and the vscode debugging extension.  You will need both.  

To try and simplify it, xdebug is a php extension that needs to run where your PHP code is running.  Again as a web application, that is likely to be either within a local webserver + php environment, or possibly within a docker container (that has the PHP server), or even on a "remote" server.

The point is, that there are many different ways to run "serverside" PHP, and for that reason, there isn't a simple answer.  

What I can say, is that xdebug is actually a multi function extension that provides

  • a step debugger
  • a profiler (for timing)
  • a code coverage analyzer
  • a function tracer

So there are a myriad of different switches and options it allows for.

Purely from the step debugging point of view you have to understand a couple of things:

  • XDebug implements a debugging protocol called DBGP
  • Any editor that can talk DBGP will work with xdebug.
  • The actual "debugging" you think of, is really more of a trick, whereby xdebug accepts information about source files and line numbers for things like breakpoints, and the editor can then use this to synchronize your view of things, provide information about variables and functions, the stack etc, but in reality it's sort of an illusion, in that the code is not ever running within VSCode.

So you have to realize that XDebug is in charge of debugging.  It does this by connecting to VSCode and at that point, will allow for communication.  So when you saw the message about a connection, it was because the editor can't do anything since there is no configuration setup to allow the INCOMING connection from the server where xdebug is installed back to VSCode.  

The problem with any how-to for setting up xdebug with vscode, is that it entirely depends on where the server with xcode installed is running.  

With that said, at this point, most people using vscode are using this extension to facilitate the configuration you need.  

Another thing to understand, is that with a web application, you typically need a way to initialize the debugging conversation, and start the debugging session from the web application.  In most cases, people will use a cookie addon that sets a special cookie in your browser, which the server will see.

There have been a number of these browser extensions over the years, but luckily at this point there is one that Jetbrains has backed here: https://github.com/JetBrains/xdebug-extension

There are versions for different browsers.  You will use this browser extension to setup the cookie needed to tell xdebug to start a debugging session in your VSCode.

Once started xdebug will try and connect back to your IDE on the port you configure.  So this means that port based networking is involved.  By default (assuming step debugging is enabled) that port is 9003.  See  xdebug.client_port in the manual.  So your IDE needs to be configured to open up port 9003, which is what the vscode extension helps you do.

I recommend you try and read through this:  https://xdebug.org/docs/step_debug

There are many links in there to information about particular environments and videos to help you try and get it working.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.