Debug PHP with Xdebug

Set up step debugging for a PHP-backed project in PhpStorm or VS Code, set a breakpoint, and pause your code mid-request to inspect it.

Xdebug lets you pause your PHP code on any line, look at every variable, and step through it one line at a time, instead of scattering var_dump() calls around. A marker you place on a line is called a breakpoint. When a request reaches it, PHP stops there and your editor shows you exactly what’s going on.

Waddle ships Xdebug ready to use. This walkthrough connects it to PhpStorm, with a shorter section for VS Code at the end. The screenshots are from PhpStorm on Windows, but the steps are the same on Linux and macOS.

Check that Xdebug is installed

Install xDebug is on by default when you create a PHP project, so most projects already have it. To check, click the paintbrush icon () on your project’s row to open the project settings, go to the PHP tab, and look for xdebug in the Extensions table.

If it’s missing, type xdebug into the table’s input, add it, click Save, and build the project again.

Waddle configures Xdebug to start only when you ask it to, so it doesn’t slow down normal page loads. Its settings live in a Git-ignored file, so Xdebug is never switched on when you deploy the project to a server. Dev-Only PHP Config explains how.

Open the project in PhpStorm

Open the whole project folder in PhpStorm, not just the website code. By default that’s ~/.waddle/projects/ followed by the project’s directory name. On Windows, the project lives inside your WSL distro, at a path like \\wsl.localhost\Ubuntu\home\<you>\.waddle\projects\<project>.

Connect PhpStorm to Docker

Open Settings and go to Build, Execution, Deployment → Docker. If the list is empty, click + to add a Docker connection.

  • On Windows, choose WSL and pick the distro Waddle uses (usually Ubuntu).
  • On Linux and macOS, the default option PhpStorm suggests is the right one.

Wait for Connection successful, then click Apply.

PhpStorm's Docker settings connected to Docker through WSL.
PhpStorm connected to Docker, here through WSL on Windows.

Add the project’s PHP as an interpreter

This step tells PhpStorm to use the exact PHP your project runs, inside its container, rather than any PHP installed on your computer.

In Settings, go to PHP. Next to CLI Interpreter, click the … button.

PhpStorm's PHP settings with the CLI Interpreter button highlighted.
The CLI Interpreter button in PhpStorm’s PHP settings.

In the CLI Interpreters window, click + and choose to add an interpreter from Docker. Then fill in:

  • Server. The Docker connection from the previous step.
  • Configuration files. The project’s docker-compose.yml and docker-compose.override.yml, the same two files Waddle uses locally.
  • Service. php.

PhpStorm often fills in some of these for you, such as the Compose files it finds in the project.

Click OK. PhpStorm starts the container and reads its PHP version. When it shows Debugger: Xdebug next to the PHP version, PhpStorm has found Xdebug and you’re set.

PhpStorm's CLI Interpreters window using the php service from Docker Compose, showing Xdebug as the debugger.
The interpreter set up from the project’s php service, with Xdebug detected.

Map the project to its path inside the container

Your files live in one place on your computer, but PHP sees them at a different path inside its container. Waddle always mounts the project at /etc/waddle/project, and PhpStorm needs to know that so it can match a running line of code to the file in your editor.

In Settings, go to PHP → Servers and click +:

  • Name and Host. Your project’s domain, for example myblog.localhost.
  • Port. 443 if your project uses HTTPS (the default), otherwise 80.
  • Debugger. Xdebug.
  • Check Use path mappings. In the table, find your project’s folder (the top-level one, not website) and set its Absolute path on the server to /etc/waddle/project.

Click OK.

PhpStorm's Servers settings mapping the project folder to /etc/waddle/project.
The project folder mapped to /etc/waddle/project.

Install a browser extension

Because Xdebug only starts when asked, your browser needs a way to ask. The easiest is an Xdebug helper extension, available for Chrome, Firefox, and Edge. Install one, open its options, and set the IDE key to PhpStorm.

Please note that an extension like this is helpful, but in no way mandatory. If you prefer, you can trigger Xdebug just as well by adding a query string to the URL, as shown further down.

The Xdebug helper extension's options with the IDE key set to PhpStorm.
The Xdebug helper options, with PhpStorm as the IDE key.

Open your site, click the extension’s bug icon in the address bar, and choose Debug. The icon turns green. From now on, every request from this tab asks Xdebug to start.

A browser address bar with the Xdebug helper's bug icon switched on.
The green bug icon means debugging is on for this site.

If you’d rather not install an extension, add ?XDEBUG_SESSION=1 to the end of a URL instead. It starts a debugging session for that one request.

Set a breakpoint and debug

Back in PhpStorm:

  1. Click Start Listening for PHP Debug Connections in the toolbar (the bug icon with a phone receiver).
  2. Open a PHP file your page runs, such as website/src/index.php, and click in the gutter next to a line number. A red dot marks the breakpoint.
  3. Reload the page in your browser.

The page stops loading, PhpStorm comes to the front, and the line with the breakpoint is highlighted. The Debug panel at the bottom shows every variable at that moment. Use the step buttons to move through the code line by line, or Resume to let the request finish.

PhpStorm paused on a breakpoint, with the Debug panel showing variables.
PhpStorm paused on a breakpoint. The listen button is highlighted in the toolbar.

The first time a request comes in, PhpStorm may ask you to accept the incoming connection, and you must accept it before debugging can start.

Using VS Code instead

Install the PHP Debug extension, open the project folder, and create .vscode/launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for Xdebug (Waddle)",
            "type": "php",
            "request": "launch",
            "port": 9003,
            "pathMappings": {
                "/etc/waddle/project": "${workspaceFolder}"
            }
        }
    ]
}

Start Listen for Xdebug (Waddle) from the Run and Debug panel, set a breakpoint, switch on your browser extension (any IDE key works), and reload the page.

If it doesn’t stop

  • The breakpoint is ignored. Check that the browser extension’s icon is green, or that the URL has ?XDEBUG_SESSION=1, and that your editor is listening.
  • PhpStorm says it can’t find the file. The path mapping is off. Map the project’s top-level folder to /etc/waddle/project, not the website folder.
  • Nothing reaches your editor at all. Xdebug connects back to your editor on port 9003. A firewall on your computer can block that connection, so allow incoming connections on port 9003 from Docker.

Next: Modifying a Template