Caveman PHP Debugging

Certainly it's possible to have nice debugging tools for php (ZEND has one), unfortunately I have not had the experience of any so I will tell you how I debug php - caveman style!

Poke it with a stick
Sometimes, you aren’t sure if the code is getting to a certain line. For quick checks I put

print "here";

That’s fine until I get interrupted or go home only to come back the next day and wonder, huh? where did I put that line??

To avoid that mess, sometimes I do this:

print __FILE__ .":".__LINE__." got to display function";

The magic constants display the file name and line number. So once I'm sure that my code is working then I can go remove this line of code. I suppose I could make a macro in my editor to do something like this easily.

Bring out the club
Sometimes you wish you had a "watch" window to see the contents of a variable at some point in time. PHP has some functions to examine a variable, print_r, var_dump, var_export. If you are using PHP 5 you might want to check them out, some have special things with PHP5 Objects. Me, I'm just lucky to have something greater than 4.0 to work with so I usually use this function to examine a variable:

function dump($v) {
     print "<pre>";
     var_dump($v);
     print "</pre>";
}

The pre tags format the data nicely and make it easier to read.Sometimes it gets buried under divs and such, so then I view source and can probably find it. Sometimes I've put it in a div and give it a background color and z-index of something like 100 so it will be displayed on top of anything on the page.

Push the boulder
In some cases its time to bring out the big guns. In which case I've used a class called Lens Debug by Php Lens. It pops up a new window and keeps a running list between page refreshes (handy to compare between two runs of the page). Pretty handy tool and you can turn it on and off - although I usually comment out the calls to the lens debug even when I turn it off so its not processing those lines, but the off feature helps to make sure nothing pops up by mistake on the production server.

I haven't tried it but Pear has a Var_Dump class that might work pretty well!

Hope those tips help, anybody else have any favorite debugging tips?

Fun with Debugging

What I like to do is write to a javascript window:

class Debug {
  function start() {
    echo '<script> myWnd = window.open("watchWindow", "Debug Window", "width=200,height=300")</script>';
  }

  function dump($string) {
     echo '<script> myWnd.document.write("'. $string .'");</script>';
  }

  function dump_var($var) {
    ob_start();
    var_dump($var);
    $output = ob_get_clean();
    echo '<script> myWnd.document.write("'. $output .'"); </script>';
  }
}


Disclaimer: This particular code is untested as I wrote it pretty spur of the moment

Thats close

Stephen, thats close to the code for the php lens debug script. :)

DebugConsole

Found this via the www.pro-php.com Podcast, it looks pretty cool! http://www.debugconsole.de/

Edit: Only works with PHP 5.0.1 or greater. DOH!