Geoff Garbers

Husband. Programmer. Tinkerer.

Quick Remote Debugging with the PHP CLI & XDebug

Oct 30, 2019

I make use of PhpStorm to build my PHP applications. It has a really nifty concept of Zero-Configuration Debugging when it comes to debugging your PHP scripts. From what I understand, it basically opens up a port on your development machine, and listens for incoming XDebug connections.

I’ve found that there are times that I need to very quickly debug a PHP CLI script from the command line, but I don’t want to go through the hassle of setting up a debug configuration in PhpStorm.

The bash function below will execute your PHP script, but will enable XDebug’s remote debugging before doing so:

xdebug() {
  php -dxdebug.remote_enable=1 -dxdebug.remote_mode=req -dxdebug.remote_port=9000 -dxdebug.remote_host=127.0.0.1 "$@"
}

It assumes the debugger is listening on 127.0.0.1:9000. Simply add this function to your ~/.bashrc or ~/.zshrc file to ensure it’s available in your shell, and then run it:

xdebug path/to/script.php