Using XDebug in VSCode
Last updated
Last updated
Using XDebug in Visual Studio Code is not difficult, but it's easy to miss one detail that prevents the whole thing from working. Here's a step-by-step tutorial:
Install the "PHP Debug" VSCode extension by Felix Becker
Go to the debug sidebar by clicking on the icon that looks like this:
Click on the gear icon to edit the launch.json
configuration file
Make sure that in the "Debug PHP" section, the port is set to "9000"
Add this section within "Debug PHP":
Note: If you include both repositories in the same workspace, the pathMappings
section would instead be:
You can also tell the debugger not to show errors from any code that is in the vendor
folder (i.e. third-party code) by defining an ignore
section, also within "Debug PHP":
You can change the name of the section from "Debug PHP" to "Debug Elentra ME", which may help if you switch projects
Save this file
Open a terminal window in your "elentra-developer" Docker container
Look at the /etc/php.d/15-xdebug.ini
file and check that the xdebug.remote_enable
option is set to "true"
That is all the setup you need, so go to VSCode and click the green "run" triangle at the top of the debug sidebar (beside "Debug PHP" or "Debug Elentra ME" if you changed the section name).
The bottom of the VSCode editor should turn orange and a small debug toolbar should appear near the top of the window. On any executable line of PHP code, click to the left of the line numbers and a small red dot should appear. This is a breakpoint, where PHP will stop before executing the line.
I use the XDebug helper extension in Firefox, so I click on the bug icon in my address bar and select "Debug"
Refresh the page, or click on a link, or do anything else that should execute the code where you placed the breakpoint. The VSCode window should bring itself to the front, with your breakpoint line highlighted. Now you can explore the variables in scope, set watches, and navigate through the call stack. Happy debugging!