SlideShare a Scribd company logo
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

More Related Content

PPT
Php Error Handling
PDF
Introduction to php exception and error management
PPTX
Handling error & exception in php
PPT
PHP - Introduction to PHP Error Handling
PDF
Php exceptions
PPT
Error reporting in php
PDF
Elegant Ways of Handling PHP Errors and Exceptions
PDF
Errors, Exceptions & Logging (PHP Hants Oct '13)
Php Error Handling
Introduction to php exception and error management
Handling error & exception in php
PHP - Introduction to PHP Error Handling
Php exceptions
Error reporting in php
Elegant Ways of Handling PHP Errors and Exceptions
Errors, Exceptions & Logging (PHP Hants Oct '13)

What's hot (18)

PDF
2 debugging-c
PPTX
PHP tutorial | ptutorial
PPT
PHP - Introduction to PHP - Mazenet Solution
PPS
Commenting Best Practices
PPT
Surprise! It's PHP :) (unabridged)
PPTX
Listen afup 2010
PDF
(8) cpp abstractions separated_compilation_and_binding_part_i
PPTX
PHP 5.3
PPT
PHP coding tips and review guides
DOC
Php tutorial
PPT
Php(report)
PPT
Php Best Practices
PDF
Winter%200405%20-%20Beginning%20PHP
PDF
Php tutorial
PPT
PDF
Preparing for the next php version
PDF
Php tutorial(w3schools)
PPTX
2 debugging-c
PHP tutorial | ptutorial
PHP - Introduction to PHP - Mazenet Solution
Commenting Best Practices
Surprise! It's PHP :) (unabridged)
Listen afup 2010
(8) cpp abstractions separated_compilation_and_binding_part_i
PHP 5.3
PHP coding tips and review guides
Php tutorial
Php(report)
Php Best Practices
Winter%200405%20-%20Beginning%20PHP
Php tutorial
Preparing for the next php version
Php tutorial(w3schools)
Ad

Similar to Debugging With Php (20)

PPTX
How to drive a malware analyst crazy
PPTX
44CON London 2015 - How to drive a malware analyst crazy
PPTX
Joomla! Day Chicago 2011 Presentation - Steven Pignataro
PPT
A Life of breakpoint
PPTX
Debugging in .Net
PPT
.NET Debugging Tips and Techniques
PPT
.Net Debugging Techniques
PPT
Everyone loves PHP
PDF
Debugging Drupal - How to Debug your Drupal Application
ODP
The why and how of moving to PHP 5.4/5.5
PPTX
PVS-Studio and static code analysis technique
PDF
Winter%200405%20-%20Beginning%20PHP
PPTX
High performance computing seminar1.pptx
DOCX
C# tutorial
PPT
Php Ppt
PPTX
Best Coding Practices For Android Application Development
ODP
Aspect-oriented programming in Perl
PDF
Unmanaged Parallelization via P/Invoke
PDF
Xdebug - Derick Rethans - Barcelona PHP Conference 2008
How to drive a malware analyst crazy
44CON London 2015 - How to drive a malware analyst crazy
Joomla! Day Chicago 2011 Presentation - Steven Pignataro
A Life of breakpoint
Debugging in .Net
.NET Debugging Tips and Techniques
.Net Debugging Techniques
Everyone loves PHP
Debugging Drupal - How to Debug your Drupal Application
The why and how of moving to PHP 5.4/5.5
PVS-Studio and static code analysis technique
Winter%200405%20-%20Beginning%20PHP
High performance computing seminar1.pptx
C# tutorial
Php Ppt
Best Coding Practices For Android Application Development
Aspect-oriented programming in Perl
Unmanaged Parallelization via P/Invoke
Xdebug - Derick Rethans - Barcelona PHP Conference 2008
Ad

Recently uploaded (20)

PPTX
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
PDF
Google’s NotebookLM Unveils Video Overviews
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
madgavkar20181017ppt McKinsey Presentation.pdf
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
PDF
Test Bank, Solutions for Java How to Program, An Objects-Natural Approach, 12...
PDF
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
PDF
Top Generative AI Tools for Patent Drafting in 2025.pdf
PDF
DevOps & Developer Experience Summer BBQ
PDF
Enable Enterprise-Ready Security on IBM i Systems.pdf
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PPTX
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
PDF
Chapter 2 Digital Image Fundamentals.pdf
PDF
CIFDAQ's Token Spotlight: SKY - A Forgotten Giant's Comeback?
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
PDF
This slide provides an overview Technology
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
PDF
Reimagining Insurance: Connected Data for Confident Decisions.pdf
PDF
Event Presentation Google Cloud Next Extended 2025
PDF
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
Google’s NotebookLM Unveils Video Overviews
Understanding_Digital_Forensics_Presentation.pptx
madgavkar20181017ppt McKinsey Presentation.pdf
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Test Bank, Solutions for Java How to Program, An Objects-Natural Approach, 12...
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
Top Generative AI Tools for Patent Drafting in 2025.pdf
DevOps & Developer Experience Summer BBQ
Enable Enterprise-Ready Security on IBM i Systems.pdf
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
Chapter 2 Digital Image Fundamentals.pdf
CIFDAQ's Token Spotlight: SKY - A Forgotten Giant's Comeback?
Automating ArcGIS Content Discovery with FME: A Real World Use Case
This slide provides an overview Technology
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Reimagining Insurance: Connected Data for Confident Decisions.pdf
Event Presentation Google Cloud Next Extended 2025
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT

Debugging With Php

  • 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