# Debugging with Xdebug

Xdebug (opens new window) is an extension for PHP, and provides a range of features to improve the PHP development experience. The setup can depend on your local development environment and which IDE you are using.

# DDEV, Xdebug and VSCODE

Xdebug can have a signifant performance hit on PHP requests, so by default we usually run local development enviroments without Xdebug enabled.

  1. enable XDebug with DDEV, assuming a default DDEV Drupal configuration:
ddev xdebug
  1. In VSCode, enable the PHP Debug extension (opens new window)

  2. Click on the Run and Debug icon to set the configuration.

  3. Cick 'create a launch.json file'

image

  1. Add the following to the launch.json file
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Listen for XDebug",
      "type": "php",
      "request": "launch",
      "port": 9003,
      "log": true,
      "pathMappings": {
        "/var/www/html": "${workspaceRoot}/MY_PROJECT",
      }
    }
  ]
}

Note that the path mapping might depend on where you workspace root is. In this example, I have a workspace into which I have created the project into the MY_PROJECT folder. DDEV runs from within the MY_PROJECT folder, so it maps that folder to /var/www/html

  1. Press the Start Debugging icon to start listening for Xdebug

image

  1. Add a breakpoint to your index.php file by clicking to the left of the line numbers of a line with code on.

image

  1. Load the home page of the site

  2. See the script break and provide you with variables and callstack.

image