Xdebug in Netbeans on the Command Line

Xdebug has become one of the main tools that I use as a php developer.  I just learned how to step through files in NetBeans while running scripts on the command line. 

For my current gig, we have several scripts that need to run that take forever to finish. I'm talking hours. Obviously, doing them through the browser would be impossible, so we use command line php scripts to make sure they can finish completely. Last Friday, I was having issues with one of these, but didn't know why it was erroring out. I could have done a bunch of var_dumps and whatever to try and figure it out, but I wanted to be able to just put a breakpoint in the script and figure out exactly where it was happening in a lot less time. 

Thankfully, the answer was pretty simple and easy to find over on Stack Overflow. The one that I ended up going with was actually the second answer. Here it is if you don't want to bother clicking through:

  1. Start the debugger in the IDE. Take note of the session name in the URL. For me, it was "netbeans-xdebug"
  2. On the command line, enter this: 
    export XDEBUG_CONFIG="idekey=netbeans-xdebug"
  3. Run your PHP file, it will trigger breakpoints in NetBeans just like running from the browser.

That's it. Super simple, but if you're using command line scripts, it's really useful. 

If you want to turn it off again, just enter this. The CLI instance of PHP will stop debugging, but your browser will still be going like normal until you stop that session:

unset XDEBUG_CONFIG 

Hope this helps people! Enjoy!

blog comments powered by Disqus