XDebug

XDebug is a code debugger that lets you stop your code while it's running, and step through line by line. You can see the value of defined variables and find where errors occur.

Enable XDebug

XDebug must be installed as a PHP extension, and enabled on your webserver.

You also need to configure it to respond to debugging requests.

Run php -i | grep xdebug to see your settings.

XDebug 3 recently came out, with many changes to configuration so be careful which instructions you look at.

I use lando: https://docs.lando.dev/config/drupal9.html#configuration

Configure IDE

Code editors like PHPStorm and VScode have relatively easy ways to set up a connection to XDebug.

There is a button to enable listening, and incoming connections will trigger a set breakpoint. PHPStorm usually pops up with a dialog to finish configuration.

There is an option to break on the first executed line which helps get set up since you may not know where in the code you can put a breakpoint yet.

Environment Variables

XDEBUG_MODE
This will override your xdebug.mode php config, so make sure to set it to be what you want, or unset it to allow php config to control the mode.

Lando will set this automatically, unless you explicity set it to empty: https://github.com/lando/lando/issues/2728#issuecomment-739988384

Path mapping

You will have a copy of the code in your IDE, while code is executing on the server. If you're working locally, this could be on your computer or in a VM. Path mapping tells XDebug how to look for the file that is being executed by the server so it can display the local copy in your IDE.

Profiling

Xdebug can "profile" an execution and provide a report on what functions were called and how long they took.

This helps identify areas of the code that are causing a bottleneck.

Tracing

Output a trace of function calls.

Level