Debug PHP-cli scripts with Xdebug and Sublime Text 2

Share on FacebookTweet about this on TwitterShare on Google+Share on LinkedInPin on PinterestShare on Reddit

In my previous post, I explained how I’ve set up debugging PHP scripts with Xdebug and Sublime Text  2 in a web based environment. In this part, I’ll outline how I debug PHP command line scripts.

If you want to follow this guide, make sure you have everything setup as explained in the previous post.

Triggering Xdebug

When using Xdebug from a web browser, I use the Chrome extension Xdebug Helper to send a valiid XDEBUG_CONFIG parameter string to the PHP process. The magic part is to set the idekey parameter to sublime.xdebug (sent via the cookie). To do the  same thing when running a script from the command line, the magic trick is to use the environment variables. This is explained in the Xdebug manual, like this:

export XDEBUG_CONFIG="idekey=session_name"
php myscript.php

Oh, how cumbersome to type. Let’s do that in a script instead. Create a file php5d like this:

export XDEBUG_CONFIG="idekey=sublime.xdebug"
php5 $@

Make the file executable:

chmod +x php5d

Then give it a try, let’s debug the test script we created in the previous post, test.php:

  1. Open test.php in Sublime Text 2
  2. Start a debugging session by hitting Shift+F8
  3. Set a breakpoint on a suitable line (must be a non-blank line)
  4. Run the script: php5d test.php

You should see something like this:


One last thing

With the php5d script in place, it’s easy and straight forward to debug command line scripts without doing too much damage on the normal environment. The last thing I did in my environment was to put the script in my /home/erik/bin folder to make it callable from everywhere on my machine:

Make sure I’ve got a central place for your personal scripts. At the end of your ~/.bashrc, make sure you have a line like this (you may already have something similar in place, I’m using subfolder bin, you may use something else):

## Additional personal scripts etc.

Then, copy the php5d script to the intended folder:

mv php5d ~/bin

And reload .bashrc (or just start a new terminal window)

. .bashrc

There you go, a globally available php5d command that triggers debugging in Sublime Text 2 for whatever PHP script you launch. Enjoy






2 thoughts on “Debug PHP-cli scripts with Xdebug and Sublime Text 2

  1. Pingback: Debugging your phpunit test cases in CodeIgniter |

Leave a Reply