PHP debugging in VSCode with Xdebug and MAMP
Configuring Xdebug that comes installed with MAMP Pro to be used from the debug panel I Visual Studio Code.
For anyone using MAMP Pro to host their local environment Xdebug comes handily installed and all it needs is some configuration to work well with VSCode. I will not cover installing Xdebug without MAMP, instead I will refer you to another article.
1. Enable Xdebug in MAMP Pro
Make sure that Xdebug is enabled in the PHP section of MAMP.
If you are using regular MAMP and not the pro version this guide is roughly equivalent to step one and two.
2. Configure Xdebug in php.ini
When still inside MAMP Pro, open File > Edit template > PHP (php.ini) and select the version of PHP you are running. Find the Xdebug section (likely on the bottom of the file) and make sure it looks like this:
The PHP version should be there already depending on what you MAMP settings are.
After saving and exiting MAMP should restart.
3. Install PHP Debug extension for VSCode
Search for PHP Debug in the VSCode extensions and install it. If you are using an older version of VSCode you may need to reload the editor after installing the extension.
Open the debug panel in the VSCode side bar. Open the dropdown where it says ”No Configuration” and click ”Add configuration. You can also click the ”gear-icon”. Choose PHP as you environment and VSCode will generate a launch.json file. The default configuration should do to begin with, for more information on how to configure PHP Debug check out the extension docs.
Automatically generate a config file for PHP Debug
Make sure PHP Debug is set to "Listen for Xdebug" and you should be good to go. Add some breakpoints, start the debugger and it will stop when that code is run.