Bookmark and Share

Remote debugging PHP using Xdebug on MAMP

Posted: Thursday, May 21st, 2009 at 10:19 pmUpdated: Thursday, May 28th, 2009 at 5:38 pm

Finally! Remote debugging the script

Running remote script with Xdebug turned on is pretty simple. All you have to do is pass XDEBUG_SESSION_START parameters and assign it the same value as the identification string you’ve setup in your Eclipse IDE. In our case, it’ll be 1. So the URL to run the script with Xdebug turned on would be http://localhost:8888/test_debug.php?XDEBUG_SESSION_START=1.

But before you do that, you need to go back to Eclipse and put a break point where you want the script to stop. For our tutorial, let’s stop the script once it hits the for loop. Make sure line 11 (the for loop) is highlighted in your Eclipse IDE. You can then press command-shift-b to activate break point on that line, or you can do it from Run -> Toggle Breakpoint or you can right click on the column just to the right of the line and select toggle break point.

Once you access the same URL with XDEBUG_SESSION_START parameter, you should see as if Apache is still downloading your script. By now, your Eclipse IDE should already be remote debugging as indicated that there’s a thread connection on the client as seen below.

You can also watch the variables on the top right box. Right now, we have variable $i and variable $divided as undefined as we’re about to execute the for loop.

Useful shortcuts when remote debugging

While you’re walking through the code, there are 3 actions that you can do:

  1. F5 : Step into.
  2. F6 : Step over.
  3. F7 : Step out.

I think it’s best for you to experience first hand the difference among the 3 actions.

F5 – Step into.

So for now, let’s hit F5 twice. Now we’re on line 12 and you can see variable $i is set to 0. Pressing F5 again, will take you into the function definition. Notice that variable $divisor is set to 0. Pressing F5 once more will take you inside the IF condition. Hitting F5 once more will take you out of the function and line 13 is about to be executed. Hit F5 once more to complete the loop.

F6 – Step over.

Now, let’s see what F6 does. This time, press F6 once. Notice that the next execution is on line 12. Pressing F6 will not take you to the function definition as it’ll step over the function. So now, the next line to be executed is line 13. Notice that $divided is already assigned with the return value of divide() function.

F7 – Step out

This key is used when you want to step out of the function call. So suppose you were pressing F5 until the line about to be executed is line 3. Pressing F7 will take you immediately to the function return and as the result, the next line to be executed is line 13.

There you have it … I hope you enjoyed this article. Please leave comments / suggestions / questions if you have. I’m looking forward to improving my solution with your comments / suggestions / questions.

Pages: 1 2 3 4

2 Responses to “Remote debugging PHP using Xdebug on MAMP”

  1. Javin @ FIX Protocol Tutorial Says:

    Nice article , In my opinion Remote Debugging is the single most feature which convinced me to migrate from Netbeans to Eclipse.

    How to setup remote debugging in Eclipse

  2. Zholeh Says:

    Hi, I had the same problem with the vabiarles view being opened causing the debugger to fail with the cryptic message. I fixed it in a very strange way:1) I changed the xdebug module from the default that came with xampp (1.7.4) with php_xdebug-2.1.0-5.3-vc6.dll.2) I also made sure that the path for xdebug in php.ini was relative instead of absolute: zend_extension = \xampp\php\ext\php_xdebug-2.1.0-5.3-vc6.dll;These two things took me from frustrated axe murderer to prancing unicorn. I’m to scared to mess about with it to determine if it was just the module change or the php.ini change. Hope this helps someone.

Leave a Reply

Time limit is exhausted. Please reload the CAPTCHA.