Your SlideShare is downloading. ×
  • Like
Debugging With Php
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Debugging With Php

  • 1,264 views
Published

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

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

Published in Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
1,264
On SlideShare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
30
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Debugging techniques for PHP
      • 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
    • 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
  • 4. 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
  • 5. Goals
    • Debug faster
    • Debug for solving problems
      • Understanding code is not the goal
  • 6. 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()
  • 7. echo()
    • Echo($var) prints out $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 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
  • 11. 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 )
  • 12. 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
  • 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
    • 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
  • 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. Debugging techniques
    • Backtracing
    • Divide and conquer
  • 17. 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
  • 18. 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.
  • 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
    • Breakpoint
    • Conditional breakpoint
    Debugging: Text Editor vs. IDE
    • Text Editor
    • var_dump() or var_dump();die();
    • If ($cond){ var_dump();//die(); }
  • 23. 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();
  • 24. Debugging: Text Editor vs. IDE
    • Development IDE
    • Watch Variable/ Expression
    • Text Editor
    • Lots of (!) var_dump()
  • 25. Human aspects of debugging
    • Tiredness
    • Confidence
    • Focus
  • 26. Human aspects of debugging
    • Tiredness
      • Get some sleep
      • Be wiling to put problem aside
  • 27. 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 
  • 28. Human aspects of programming
    • Focus
      • Debugging requires concentration
      • If you won't have time at your hand, don't start going into something
  • 29. Other debugging tools
    • CVS/ Subversion/ Git/ Diff
    • Firebug net console/ Fiddler/ Wireshark
    • FirePHP
    • Issue tracker