KEMBAR78
Debugging With Php | ODP
Debugging techniques for PHP Jochen Daum Automatem Ltd PHPUG Auckland meetup 20 Oct 2009
Overview My background  PHP functions Error and logging set-up Debugging techniques The human side of debugging – you! Non-code debugging tools
My background ASP 3.0 experience since 1997 Required “website on a CD” in 2001 Luckily can't do that with ASP ;) Backend programmer for website company Styrofirm in 2002 Build and maintained PHPDbEditTk (Sourceforge) for fast scaffolding 4 major systems still in operation and maintenance now Started Automatem in 2004
My (debugging) background Only had the luxury to build everything from scratch when I was employed with Cabletalk Other times had to work with other people's code
Goals Debug faster Debug for solving problems Understanding code is not the goal
PHP facilities overview echo() / var_dump() / print_r() / die () debug_backtrace ()/debug_print_backtrace() get_defined_vars () / get_declared_classes() / method_exists() Error display and logging isset()
echo() Echo($var) prints out $var Prints “Array” for arrays (Catchable) fatal error for objects Don't use for debugging, waste of time.
var_dump() / print_r() Prints variable and its type Works with all types including arrays, objects, resources Unlimited depth Shows references var_dump <---> print_r output differences
var_dump()/print_r() by mail() Useful when debugging a live site var_dump() needs use of output buffering print_r can return value: print_r($var,true);
debug_backtrace() Prints stack trace of function calls with variables passed Shows file, line number, class, object, passed variables Can easily get messy (see example) Returns array. print_debug_backtrace() prints right away, but also messy
my_debug_backtrace() Prefer to see only line numbers my_debug_backtrace() (for code see http://nz.php.net/manual/en/function.debug-backtrace.php#85915 )
Error types http://nz.php.net/manual/en/errorfunc.constants.php E_ERROR Fatal run-time errors. Script stops. Run out of memory using array as object etc E_PARSE Compile-time parse errors. Syntax errors  E_WARNING Run-time warnings Database connection failed, division by 0 4  E_NOTICE (integer) Notices
Error Types (2) http://nz.php.net/manual/en/errorfunc.constants.php E_WARNING Run-time warnings Database connection failed Division by 0 4  E_NOTICE (integer) Notices Variable not declared Typo Array index missing
Error Logging Can be configured in php.ini or through ini_set() (nearly all of them) display_errors: 0,1,'stderr' (fcgi only) Should be disabled on live site Enabled on development site,  otherwise no error shown at all! Doesn't affect Fatal Errors
Error Logging (1) log_errors: 0,1 to log errors to a file error_log: 'syslog', file_path File to send logs to log_errors_max_len, ignore_repeated_errors, ignore_repeated_source Configuration options of how errors get logged
Debugging techniques Backtracing Divide and conquer
Back tracing Start where output is created: Actual html, json or Xml output Error message/ fatal error Go back step by step until the source is found, for example of: A variable value A problem Example: backtrace through Joomla site
Divide and conquer Assume that code is a black box, you don't need to understand it Look at debug_backtrace Start debugging in the middle of the list of functions. Work your way either way of the code.
My code guidelines ...as they relate to debugging Have a coding style and be consistent Indenting Where the brackets go directory/file structure E_NOTICE on! Takes a bit of work, but worth it
My code guidelines (2) No if without else “the code will never go here” - are you sure? No switch without default No foreach without if
My code guidelines (3) Reduce lines of code Use libraries Centralise everything Don't change if not broken
Development IDE Breakpoint Conditional breakpoint Debugging: Text Editor vs. IDE Text Editor var_dump() or var_dump();die(); If ($cond){   var_dump();//die(); }
Debugging: Text Editor vx. IDE Development IDE Stack trace Inspect array/object value Text Editor print_r(debug_backtrace());//die(); Complicated/ lots of var_dump();
Debugging: Text Editor vs. IDE Development IDE Watch Variable/ Expression Text Editor Lots of (!) var_dump()
Human aspects of debugging Tiredness Confidence Focus
Human aspects of debugging Tiredness Get some sleep Be wiling to put problem aside
Human aspects of debugging Confidence Generally start with smaller projects Plan more time if you solve a certain kind of problem first time Helps to be familiar with some frameworks and CMS systems: you will notice how they all use similar approaches 
Human aspects of programming Focus Debugging requires concentration If you won't have time at your hand, don't start going into something
Other debugging tools CVS/ Subversion/ Git/ Diff Firebug net console/ Fiddler/ Wireshark FirePHP Issue tracker

Debugging With Php

  • 1.
    Debugging techniques forPHP Jochen Daum Automatem Ltd PHPUG Auckland meetup 20 Oct 2009
  • 2.
    Overview My background PHP functions Error and logging set-up Debugging techniques The human side of debugging – you! Non-code debugging tools
  • 3.
    My background ASP3.0 experience since 1997 Required “website on a CD” in 2001 Luckily can't do that with ASP ;) Backend programmer for website company Styrofirm in 2002 Build and maintained PHPDbEditTk (Sourceforge) for fast scaffolding 4 major systems still in operation and maintenance now Started Automatem in 2004
  • 4.
    My (debugging) backgroundOnly had the luxury to build everything from scratch when I was employed with Cabletalk Other times had to work with other people's code
  • 5.
    Goals Debug fasterDebug for solving problems Understanding code is not the goal
  • 6.
    PHP facilities overviewecho() / var_dump() / print_r() / die () debug_backtrace ()/debug_print_backtrace() get_defined_vars () / get_declared_classes() / method_exists() Error display and logging isset()
  • 7.
    echo() Echo($var) printsout $var Prints “Array” for arrays (Catchable) fatal error for objects Don't use for debugging, waste of time.
  • 8.
    var_dump() / print_r()Prints variable and its type Works with all types including arrays, objects, resources Unlimited depth Shows references var_dump <---> print_r output differences
  • 9.
    var_dump()/print_r() by mail()Useful when debugging a live site var_dump() needs use of output buffering print_r can return value: print_r($var,true);
  • 10.
    debug_backtrace() Prints stacktrace of function calls with variables passed Shows file, line number, class, object, passed variables Can easily get messy (see example) Returns array. print_debug_backtrace() prints right away, but also messy
  • 11.
    my_debug_backtrace() Prefer tosee only line numbers my_debug_backtrace() (for code see http://nz.php.net/manual/en/function.debug-backtrace.php#85915 )
  • 12.
    Error types http://nz.php.net/manual/en/errorfunc.constants.phpE_ERROR Fatal run-time errors. Script stops. Run out of memory using array as object etc E_PARSE Compile-time parse errors. Syntax errors E_WARNING Run-time warnings Database connection failed, division by 0 4 E_NOTICE (integer) Notices
  • 13.
    Error Types (2)http://nz.php.net/manual/en/errorfunc.constants.php E_WARNING Run-time warnings Database connection failed Division by 0 4 E_NOTICE (integer) Notices Variable not declared Typo Array index missing
  • 14.
    Error Logging Canbe configured in php.ini or through ini_set() (nearly all of them) display_errors: 0,1,'stderr' (fcgi only) Should be disabled on live site Enabled on development site, otherwise no error shown at all! Doesn't affect Fatal Errors
  • 15.
    Error Logging (1)log_errors: 0,1 to log errors to a file error_log: 'syslog', file_path File to send logs to log_errors_max_len, ignore_repeated_errors, ignore_repeated_source Configuration options of how errors get logged
  • 16.
  • 17.
    Back tracing Startwhere output is created: Actual html, json or Xml output Error message/ fatal error Go back step by step until the source is found, for example of: A variable value A problem Example: backtrace through Joomla site
  • 18.
    Divide and conquerAssume that code is a black box, you don't need to understand it Look at debug_backtrace Start debugging in the middle of the list of functions. Work your way either way of the code.
  • 19.
    My code guidelines...as they relate to debugging Have a coding style and be consistent Indenting Where the brackets go directory/file structure E_NOTICE on! Takes a bit of work, but worth it
  • 20.
    My code guidelines(2) No if without else “the code will never go here” - are you sure? No switch without default No foreach without if
  • 21.
    My code guidelines(3) Reduce lines of code Use libraries Centralise everything Don't change if not broken
  • 22.
    Development IDE BreakpointConditional breakpoint Debugging: Text Editor vs. IDE Text Editor var_dump() or var_dump();die(); If ($cond){ var_dump();//die(); }
  • 23.
    Debugging: Text Editorvx. IDE Development IDE Stack trace Inspect array/object value Text Editor print_r(debug_backtrace());//die(); Complicated/ lots of var_dump();
  • 24.
    Debugging: Text Editorvs. IDE Development IDE Watch Variable/ Expression Text Editor Lots of (!) var_dump()
  • 25.
    Human aspects ofdebugging Tiredness Confidence Focus
  • 26.
    Human aspects ofdebugging Tiredness Get some sleep Be wiling to put problem aside
  • 27.
    Human aspects ofdebugging Confidence Generally start with smaller projects Plan more time if you solve a certain kind of problem first time Helps to be familiar with some frameworks and CMS systems: you will notice how they all use similar approaches 
  • 28.
    Human aspects ofprogramming Focus Debugging requires concentration If you won't have time at your hand, don't start going into something
  • 29.
    Other debugging toolsCVS/ Subversion/ Git/ Diff Firebug net console/ Fiddler/ Wireshark FirePHP Issue tracker