PHP Debugging










PHP Debugging

Finding and reducing defects,
increasing code quality and becoming incredibly wealthy

What is debugging?

A methodical process of finding and reducing the number of bugs or defects.

Types of debugging

Print logging

  • Beware being unable to find die() and var_dump() statements!
  • Consider simple logging class that can be enabled/disabled (Zend_Log is recommended, but is heavyweight)
  • Pretty goddamnn rudimentary, should be coalesced and output in an "authorised-only footer", overlay or email to avoid mangling program output
  • Print logging

        <?php
    
        $array = array(
            "foo" => "bar",
            "bar" => "foo",
        );
    
        var_dump($array); die(__FILE__ . ' :: ' . __LINE__);
        

    Print logging

        array(2) { ["foo"]=> string(3) "bar" ["bar"]=> string(3) "foo" }
        C:\Users\Andy\Documents\My Dropbox\_dev\vhosts\tmp.localh0.st\talk-PhpDebugging\test.php :: 14
        

    Print logging with XDebug

       array
         'foo' => string 'bar' (length=3)
         'bar' => string 'foo' (length=3)
       
        C:\Users\Andy\Documents\My Dropbox\_dev\vhosts\tmp.localh0.st\talk-PhpDebugging\test.php :: 13
        

    FirePHP

  • Firebug extension for firefox, also available for chrome firephp-chrome
  • Nicer print debugging, XDebug-esque output formatting
  • Can be enabled in production (IP whitelist, application auth, server config) without risk of changing program output (HTTP header-based)
  • FirePHP

    FirePHP

    Log Files/rSyslog

  • Passive logging - traditionally restricted to a single message per line
  • Can be useful for unknown errors in production, although emails/metrics gathering probably superior
  • Retricted verbosity means this is not usually a useful debugging technique, although it can help to indicate problems
  • Remote (runtime) Debugging

  • The most valuable form of debugging (remote to process, not system)
  • Halt program execution, examine state, evaluate expressions, alter code
  • Very powerful - when exposed to the complexity of the flow of some software, the brain may melt
  • Remote (runtime) Debugging

    Remote (runtime) Debugging

    Xdebug

    Remote (runtime) Debugging

    Xdebug

     










    Go forth and Remotely Debug your PHP!