Xdebug PHP and Command Line Scripts

I had a requirement to use Xdebug to step through a PHP script that is being run on the command line.

Oh no I thought, this is bound to be really complicated to figure out because I normally use the web browser and I have never done this before.

Pleasant surprise, its actually really easy to do this, all you need to do on the command line is run the following command:

export XDEBUG_CONFIG="idekey=netbeans-xdebug"

Then get your IDE (PHPStorm for me) listening for Xdebug and then run your PHP file on the command line.

php ./myfile.php

That’s it, it just works.

One caveat (which I actually quite like) is that it will now ALWAYS get picked up by xdebug which could get in your way. To stop this, simply unset the variable like this: