Getting rid of var_dump()… Debugging in PHP!

Did you ever wanted to toggle breakpoints, view or even edit the variable’s content during execution of your php script? Sick and tired of „var_dump()“ and „echo“ scattered all around your code? Then you should keep reading…

First of all, you need to install new software 🙂 Here’s what you gonna need:

  1. Eclipse with a PHP Extension (Netbeans might also work, but this tutorial is based in Eclipse), if you do have eclipse installed already, you might considering installing the PDT (PHP Development Tools) Plugin through the eclipse update workflow („Help“ > „Install new Software“ and search for PDT)
  2. PHP 5.3, if you’re running Snow Leopard you can skip this, otherwise RTMF on http://php.net/index.php 😉
  3. XDebug which can be found here, installation instructions can be found here

So, after you finished the installation of the tools above, it’s time to configure xdebug. Open the file „/private/etc/php.ini“ with root privileges and add the following lines at the end and in this order:

[xdebug] zend_extension=/the/path/to/your/xdebug.so  xdebug.remote_enable=On xdebug.remote_handler=dbgp xdebug.remote_host="localhost" xdebug.remote_port=9000“

Now you need to restart your apache web server either with „sudo apachectl restart“ or go to „System Preferences“ > „Sharing“ and disable and re-enable „Web Sharing“. To verify that XDebug is running correctly, save a simple php script (<?php phpinfo; ?>) in the web server’s document directory as „phpinfo.php“. In Mac OS X this is usually „/Library/WebServer/Documents/„ and go to http://localhost/phpinfo.php. If you can see something like this somewhere on the page

everything should be fine. Now let’s configure eclipse. Go to „Eclipse Preferences“ > „General“ > „Web Browser“ and select the browser of your choice. Make sure that under „PHP“ > „Debug“ > „Installed Debuggers“ XDebug is listed.

And now the „fun“ part. Create a new project and a new php file like this:

<?php

function boohoo($nice, $comment) {

$temp = $nice . $comment;

return $temp; // create breakpoint here

}

$nice = “if this is much better than var_dump “;

$comment = “please leave a comment or retweet”;

echo boohoo($nice, $comment);

?>

And create a break point on the line 6 „return $temp;“ by double clicking on the line number.

To run the script just click on the small bug and choose „Debug configurations…“

By double clicking on „PHP Web Page“ you will get a new configuration of this type. Now enter a name for this configuration, select XDebug in the „Server Debugger“ drop down menu and choose your script accordingly. (You may also toggle the „break at First Line“ checkpoint.)

Now you are officially ready to debug 🙂 After clicking „Debug“, you should be able to walk through your script step by step using either F5 or the small arrow icons in the debug window:

To adjust data double click on the variable’s value and enter the new stuff. If you have questions or suggestions please leave a comment.

Advertisements

5 Comments

Filed under Development, Eclipse, Mac OS X Software, PHP, XDebug

5 responses to “Getting rid of var_dump()… Debugging in PHP!

  1. marv

    for me (using linux) it works only if the “zend_extension” line is above the “[xdebug] block.

    zend_extension=/the/path/to/your/xdebug.so
    [xdebug]

    mfg

  2. Nice tut mate. On the Mac you’d use MacGDBp [1] for this, though. 🙂

    Beeing a native Cocoa application, it is way faster than I-eat-CPU-cycles-for-breakfast-Eclipse. Besides, if you happen to debug not only single scripts but complex applications whose request are routed through a single file, I found MacGDBp to better capture the actual files beeing executed.

    [1] http://www.bluestatic.org/software/macgdbp/

  3. That is nice to definitely find a site where the blogger knows what they are talking about.

  4. Pingback: 2010 in review | neverbeenbetter

  5. Pingback: Debug Php On Mac | More More Pics

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s