Debugging PHP in VS Code
There seems to be a lot of articles on debugging PHP on VS Code, but all of them seems to have a lot of steps and a bit complicated.
So here is a simple guide to debug your PHP code on VS Code.
What you need,
- Visual Studio Code; I’m using Version: 1.94.2
- PHP Debug and PHP Extension Pack extensions for VS Code
- Project setup; I’m using a Drupal project running on DDEV. But you can use any other setup as long as it has Xdebug — Debugger running.
Following are my projects Xdebug version and PHP versions for your reference.
$:/var/www/html$ php --ini | grep xdebug
/etc/php/8.2/cli/conf.d/20-xdebug.ini,
$:/var/www/html$ php -v
PHP 8.2.20 (cli) (built: Jun 8 2024 21:32:49) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.20, Copyright (c) Zend Technologies
with Zend OPcache v8.2.20, Copyright (c), by Zend Technologies
with Xdebug v3.2.2, Copyright (c) 2002-2023, by Derick Rethans
Step 1
Make sure PHP Debug and PHP Extension Pack extensions for VS Code are enabled on VS Code
Step 2
In VS Code, click on the Debugger icon on the left side. And select create a launch.json file and then select PHP.
Step 3
A new folder .vscode will be created with launch.json file. In that file it will be prepopulated with different debugging option boilerplate code.
But we will be using the “Listen for Xdebug” option.
And for it to work, all it needs is the path to your project directory.
In my case, the working directory in my DDEV environment is /var/www/html
$:/var/www/html$ pwd
/var/www/html
Simply add the pathMappings
property as follows;
"pathMappings": {
"/var/www/html": "${workspaceFolder}"
},
Step 4
Simply place the debug point on your PHP file and select Listen for Xdebug.
And that’s it.
Hope this helps and get those bugs crushed !