Debugging PHP in Visual Studio Code
Debugging PHP in Visual Studio Code
If you are a PHP developer who has worked with Microsoft technologies in the past, you would certainly
miss its Visual Studio editor with full debug support: F5 to run the debugger, F6 to step next, F7 to step into,
F8 to step out, etc. It simply works like magic.
The good news is that there are a few open source tools out there that can give PHP developers the same
debugging experience.
Remember PHP var_dump()? It was the only tool of the trade when it came to debugging PHP back in the
day.
The first thing we did was turn on display_errors. We would do this either in the php.ini file or at the
beginning of the code:
ini_set(‘display_errors’, ‘On’);
error_reporting(E_ALL);
We would also have certainly relied on the handy dandy echo command to watch any variable values, and
then diligently remove or comment them out once we were done. This is pretty much how all PHP
applications were troubleshooted during the pre-xdebug days.
Enter XDebug.
#What is XDebug
XDebug is a PHP extension that provides debugging capabilities for programming IDE. It was first released in
May 2002 by Derick Rethans. At the time of this writing, the latest version( 2.5.3.) of XDebug has become the
de facto standard as it is the only debugging tool in existence for PHP. Thank you, Derick! My life has never
been the same.
Set/Remove breakpoints
Perform an automatic stack trace
Set a manual variable watch
Enable function call logging
Enable profiling
Allow remote debugging
Before the install, I recommend to start with the XDebug installation wizard. It does an excellent job of
analyzing your PHP environment, then gives you tailored installation instructions.
To configure PHP to use XDebug, add the line zend_extension=path/to/xdebug to your php.ini.
xdebug.remote_enable = 1
xdebug.remote_autostart = 1
eg.
I'm going to share a little trick I use to install XDebug on a Mac. Here’s the one liner in OSX that enables the
PHP XDebug extension.
Finally, verify your installation by checking your phpinfo() output to see if there’s an XDebug section.
#XDebug in Visual Studio Code
Visual Studio Code is a free, open source, cross-platform lightweight code editor from the software giant. It
is available for Windows, Linux and OS X. You can download it here.
Now that we should have already installed XDebug on our server, we need to get the debug client installed
in VS Code so we can set the breakpoints in the code that will be hit when PHP is processing the request. To
do this, we need to get the "PHP Debug" Extension from VS Code. An easier way is to go to the Extensions
tab, and search for "PHP Debug", and then click Install.
Once the installation is complete, be sure to restart VS Code before you start using it.
To add a breakpoint, click to the left of the line number or, once you have selected a line, press F9 on your
keyboard.
Finally click the green "Play" icon or press F5 on the keyboard to start the debugging.
Now, open your web browser and visit the webpage where we set the breakpoints earlier. If everything has
been set up correctly, as soon as the program hits the first breakpoint, you should be immediately switched
back to VS Code.
Now you can use the buttons in the debug bar to proceed with debugging. Learn to use keyboard
shortcuts! The highlighted yellow line indicates where execution stopped in our PHP script.
Pay close attention to the VARIABLES and CALL STACK on the left-hand pane. They are like having a dynamic
PHP var_dump() command at your disposal.
In reality, you can set as many breakpoints as you need. To STEP OVER to the next breakpoint, press F10
on your keyboard; to STEP INTO the current line breakpoint, press F11.
#Final Thoughts
Visual Studio Code provides great PHP language support right out of the box. Debugging PHP with VS Code
is surprisingly smooth. My first time debugging was like debugging C# in the good old days. Step in, out,
over, watch etc., all work like charm!
One of the things I’ve learned is that where you put breakpoints for scripts inside MVC such as Laravel can
be tricky. In my experience the best place to put breakpoints is inside the model where all database actions
take place. Step-in doesn’t always work when debugging in MVC because of the routing.