Debugging With Php

1,520 views
1,435 views

Published on

Debugging with PHP. Talk by Jochen Daum at Auckland PHPUG meetup 20 Oct 2009, 6:00 pm NDT

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,520
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
32
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Debugging With Php

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

×