How to debug php code in visual studio code

Visual Studio Code is a great editor for PHP development. You get features like syntax highlighting and bracket matching, IntelliSense (code completion), and snippets out of the box and you can add more functionality through community-created VS Code extensions.

Linting

VS Code uses the official PHP linter (php -l) for PHP language diagnostics. This allows VS Code to stay current with PHP linter improvements.

Tip: Using XAMPP? Install the full version of PHP in order to obtain the development libraries.

There are three settings to control the PHP linter:

  • php.validate.enable: controls whether to enable PHP linting at all. Enabled by default.
  • php.validate.executablePath: points to the PHP executable on disk. Set this if the PHP executable is not on the system path.
  • php.validate.run: controls whether the validation is triggered on save (value: "onSave") or on type (value: "onType"). Default is on save.

To change the PHP settings, open your User or Workspace Settings (⌘, (Windows, Linux Ctrl+,)) and type 'php' to filter the list of available settings.

How to debug php code in visual studio code

To set the PHP executable path, select the Edit in settings.json link under PHP > Validate: Executable Path, which will open your user settings.json file. Add the php.validate.executablePath setting with the path to your PHP installation:

Windows

{
  "php.validate.executablePath": "c:/php/php.exe"
}

Linux and macOS

{
  "php.validate.executablePath": "/usr/bin/php"
}

Snippets

Visual Studio Code includes a set of common snippets for PHP. To access these, hit ⌃Space (Windows, Linux Ctrl+Space) to get a context-specific list.

How to debug php code in visual studio code

PHP extensions

There are many PHP language extensions available on the VS Code Marketplace and more are being created. You can search for PHP extensions from within VS Code in the Extensions view (⇧⌘X (Windows, Linux Ctrl+Shift+X)) then filter the extensions dropdown list by typing 'php'.

How to debug php code in visual studio code

Disable built-in PHP support

To disable the built-in PHP smart completions in favor of suggestions from an installed PHP extension, uncheck PHP > Suggest: Basic, which sets php.suggest.basic to false in your settings.json file.

Debugging

PHP debugging with XDebug is supported through a PHP Debug extension. Follow the extension's instructions for configuring XDebug to work with VS Code.

Next steps

Read on to find out about:

  • Extension Marketplace - Browse the extensions others have shared
  • Debugging - Learn more about VS Code debugging

9/1/2022

March 17th, 2016

No developer enjoys debugging web applications. But we all know that if we want quality software debugging is a crucial aspect of development. While .Net and Java developers often have IDEs that contain full debugging support, PHP developers may sometimes feel left out. Sure, there are quite a few commercial applications that help with debugging PHP. But these commercial applications are usually quite expensive and operating system dependent. Luckily there are a few open source tools out there that will give PHP developers the same debugging experience without the use of an expensive IDE.

Microsoft has recently released an open source code editor named Visual Studio Code. Visual Studio Code runs on Linux, Windows, and OSX and also features integrated debugging for almost any programming language. While Code does not feature out of the box debugging for all languages, extensions have been developed for most of the popular languages including PHP.

Installing Visual Studio Code

To get started first visit https://code.visualstudio.com to download and install Visual Studio Code.  Once Code is installed and running you will need to install the PHP debugging extension.  To do this, choose “View” on the toolbar and select “Command Palette”.  In the drop down box type “ext install” and press enter.

How to debug php code in visual studio code

Then type “PHP Debug” and choose the extension aptly named “PHP Debug” and press enter.

How to debug php code in visual studio code

Once the debugging extension is installed Visual Studio Code will restart.

Installing Xdebug PHP extension

The next step is to install the Xdebug PHP extension.  To do this visit https://xdebug.org/download.php and follow the instructions.  If the downloading of these files and modification of your php.ini seems too daunting you can also visit https://xdebug.org/wizard.php.  This wizard allows you to paste the contents of a phpinfo() call and it will then give you exact instructions on what files to download, where to place them, and what needs to be added to you php.ini file to install the extension correctly.

How to debug php code in visual studio code

Visual Studio Code debugging setup

Now we need to setup Visual Studio Code to allow debugging.  In Visual Studio Code, select “File” and then “Open Folder” and choose the folder containing your PHP code.  Choose the Debug View from the left hand side and then click the Debug button to configure our debugging environment.

How to debug php code in visual studio code
  (Debug View)       
How to debug php code in visual studio code
 (Debug)

The first time the Debug button is selected it will create a debugging configuration file.  For our purposes we can just leave it as is and not worry about it again.  Click the Debug button again and if everything is setup correctly Visual Studio Code will now be in debugging mode!

Debugging!  Finally!

For the debugger to be useful we now need to add some breakpoints in our code.  To create a breakpoint simply click in the gutter next the line you want to create a breakpoint on.

How to debug php code in visual studio code

Like most debuggers, breakpoints in Visual Studio Code are indicated by a red dot.  If we now load the page in the browser the execution of the page will stop at our breakpoint and we can inspect the current state of the page.

How to debug php code in visual studio code

(Paused at breakpoint)

How to debug php code in visual studio code

(Local and global variables)

How to debug php code in visual studio code

(Call stack)

This is a trivial example but it shows some of the debugging tools available for PHP in Visual Studio Code.  The setup may seem a little complex but just remember that it only has to be done once.  And the time spent in setup will more than be saved when it comes to time to debug your code.  Happy debugging!

How to debug php code in visual studio code

bswartz

Brian specializes in .Net and mobile software development at Accella. He graduated from Texas Tech University with a degree in computer science. While attending Texas Tech University, he took a job as a developer for a medical software company where he continued to work for 15 years. He specialized in developing interfaces for lab machines and clinical systems.

Search

Mobile App Tech Alerts

We let you know when mobile changes and advancements happen and how they impact your business.

SIGN UP

Recent Posts

  • Increase Your Association’s Visibility and Value During the COVID-19 Pandemic
  • Improving App Engagement: iOS14 Introduces Widgets, App Clips & Improved iPAD/MacOS Portability
  • Keep Calm and Remote On
  • Progressive Web Apps (PWAs) Part 5: To PWA or Not to PWA?
  • Progressive Web Apps (PWAs) Part 4: An Executive’s POV

Tags

Can you Debug PHP in Visual Studio code?

PHP debugging with XDebug is supported through a PHP Debug extension. Follow the extension's instructions for configuring XDebug to work with VS Code.

How do I Debug PHP code?

Debugging Session.
Start the ide and open the file that contains the source code that you want to debug..
Set a breakpoint at each line where you want the debugger to pause. ... .
In the Projects window, navigate to the current project node, click the right mouse button, and choose Debug from the popup menu..

How do I Debug code in Visual Studio code?

Let's review these one by one:.
Continue F8. You can use the continue F8 button to resume the program's execution when it pauses at a breakpoint. ... .
Step over F10. ... .
Step into F11. ... .
Step out Shift+F11. ... .
Restart Ctrl+Shift+F5. ... .
Stop Shift+F5..

How do I get PHP code to work in Visual Studio?

There is a much easier way to run PHP, no configuration needed:.
Install the Code Runner Extension..
Open the PHP code file in Text Editor. use shortcut Ctrl+Alt+N. or press F1 and then select/type Run Code , or right click the Text Editor and then click Run Code in editor context menu..