The Strange World of
  Debugging PHP
    Refresh Cambridge, 1 July 2009

       Simon R Jones, Studio 24
          www.stu...
We’re trying to avoid this




                      White screen of death
Which makes us feel like this




       http://www.flickr.com/photos/nebarnix/320901099/
While we’d rather be happily
getting on with development




         http://www.flickr.com/photos/sjaek/468245227/
Debugging PHP

•   What is a bug?

•   Common errors

•   Error reporting

•   Basic debugging techniques

•   A proper PH...
What is a bug?

•   Human error (typos)

•   Logic errors

•   Environmental errors (files, web service, db)

•   And rarel...
Common errors
Syntax errors
•   Something’s usually in the wrong place..
    •   T_ENCAPSED_AND_WHITESPACE
Syntax errors
• T_NUM_STRING
• T_OBJECT_OPERATOR
• T_STRING
• T_VARIABLE
• T_PAAMAYIM_NEKUDOTAYIM !
 (means double colon i...
Whose line is it anyway?



• The syntax error isn’t always on the line
  reported
• Usually the case with missing quotes
Headers already sent


•   PHP mime type = text/html

•   A single space after a closing ?> will send
    HTML + headers t...
Error Reporting
Recommended settings
        Production (live) site
  •   Hide on screen
  •   Display friendly error page
  •   Log error...
Display errors


•   Default php.ini setting is to hide errors

•   Override in first PHP file

•   A gotcha: any syntax err...
Bitwise operators


• Report all errors..
• But not notices
• And not warnings
• http://php.net/manual/en/function.error-
...
Error types

• Parse error
• Fatal error
• Warning
• Notice
• Strict notice
Error log
• Default is to log errors to the Apache
  ErrorLog location
  (usually set in a VirtualHost directive)
• A gotc...
Custom error reporting
•   set_error_handler()

•   Set a callback function (or class) to manage
    errors

•   Show nice...
Throwing errors
• Throw errors in your code (i.e. if a
  database call fails)
• trigger_error()
• Exceptions in PHP 5
• Al...
Supressing errors
• @ operator
• Arrgh, not generally a good idea
• Sometimes a valid use case (i.e. throw an
  exception ...
Watch out!
Errors suppressed with the @
operator are still sent to a
custom error handler. Can cause
headaches and unexpla...
Exceptions


• PHP 5 only
• Only classes can throw an exception
• set_exception_handler() to deal with
  uncaught exceptio...
Pretty Blue Screen




• Nice exception handler from Harry Fuecks
• http://www.sitepoint.com/blogs/2006/04/04/
  pretty-bl...
Basic debugging
  techniques
var_dump
 • Tracking variables throughout code
 • Add formatting:



• Better: Zend_Debug::dump()
Debug window




• Javascript debug window
• http://devzone.zend.com/article/1256
Firebug console logging


• Firebug and FirePHP plugins for Firefox
• Doesn’t send output to HTML page so
  won’t interfer...
ZFDebug


• Zend Framework plugin to display debug
  information (time spent, memory usage,
  database queries, etc)
• htt...
A proper debugger
• Step through your code line by line
• Set breakpoints
• View environment, script variables and
  funct...
Xdebug

• Open Source
• Packaged with Komodo IDE
• Or via http://www.xdebug.org/
Zend Debugger

• Commercial
• Part of Zend Studio
• Debugging from Firefox or from within
  Zend Studio
Best practises
•   Use an IDE with PHP syntax checking

•   Enforce coding standards

•   Comment your code! (PHPDoc)

•  ...
Links
•   Zend Studio - www.zend.com/studio

•   Eclipse + PDT - www.zend.com/en/community/pdt

•   Komodo - www.activesta...
Thanks!


Simon R Jones, Studio 24, www.studio24.net
The Strange World of PHP Debugging
The Strange World of PHP Debugging
The Strange World of PHP Debugging
Upcoming SlideShare
Loading in...5
×

The Strange World of PHP Debugging

7,683

Published on

A talk I gave at Refresh Cambridge on 1st July, 2009, on PHP Debugging.

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

No Downloads
Views
Total Views
7,683
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
4
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

The Strange World of PHP Debugging

  1. 1. The Strange World of Debugging PHP Refresh Cambridge, 1 July 2009 Simon R Jones, Studio 24 www.studio24.net
  2. 2. We’re trying to avoid this White screen of death
  3. 3. Which makes us feel like this http://www.flickr.com/photos/nebarnix/320901099/
  4. 4. While we’d rather be happily getting on with development http://www.flickr.com/photos/sjaek/468245227/
  5. 5. Debugging PHP • What is a bug? • Common errors • Error reporting • Basic debugging techniques • A proper PHP debugger • Best practises
  6. 6. What is a bug? • Human error (typos) • Logic errors • Environmental errors (files, web service, db) • And rarely, it’s a system bug (PHP, Apache)!
  7. 7. Common errors
  8. 8. Syntax errors • Something’s usually in the wrong place.. • T_ENCAPSED_AND_WHITESPACE
  9. 9. Syntax errors • T_NUM_STRING • T_OBJECT_OPERATOR • T_STRING • T_VARIABLE • T_PAAMAYIM_NEKUDOTAYIM ! (means double colon in Hebrew)
  10. 10. Whose line is it anyway? • The syntax error isn’t always on the line reported • Usually the case with missing quotes
  11. 11. Headers already sent • PHP mime type = text/html • A single space after a closing ?> will send HTML + headers to the browser • Don’t include the closing ?> tag (unless you’re outputting HTML)
  12. 12. Error Reporting
  13. 13. Recommended settings Production (live) site • Hide on screen • Display friendly error page • Log errors Development site • Display on screen
  14. 14. Display errors • Default php.ini setting is to hide errors • Override in first PHP file • A gotcha: any syntax errors in this file will ignore these settings • Error reporting is a bitwise setting
  15. 15. Bitwise operators • Report all errors.. • But not notices • And not warnings • http://php.net/manual/en/function.error- reporting.php
  16. 16. Error types • Parse error • Fatal error • Warning • Notice • Strict notice
  17. 17. Error log • Default is to log errors to the Apache ErrorLog location (usually set in a VirtualHost directive) • A gotcha: this overrides php.ini error_log setting which can be confusing • Can be overridden with PHP options log_errors and error_log
  18. 18. Custom error reporting • set_error_handler() • Set a callback function (or class) to manage errors • Show nice error page to users on live site • Email critical errors to the tech team • http://uk3.php.net/manual/en/function.set-error- handler.php
  19. 19. Throwing errors • Throw errors in your code (i.e. if a database call fails) • trigger_error() • Exceptions in PHP 5 • Always program defensively, don’t assume that database call will always work or a certain field will exist in an external XML file
  20. 20. Supressing errors • @ operator • Arrgh, not generally a good idea • Sometimes a valid use case (i.e. throw an exception on function failure) • Scream to disable supressed errors! •ini_set('scream.enabled', 1);
  21. 21. Watch out! Errors suppressed with the @ operator are still sent to a custom error handler. Can cause headaches and unexplained bugs!
  22. 22. Exceptions • PHP 5 only • Only classes can throw an exception • set_exception_handler() to deal with uncaught exceptions • Uncaught exceptions raise a Fatal error
  23. 23. Pretty Blue Screen • Nice exception handler from Harry Fuecks • http://www.sitepoint.com/blogs/2006/04/04/ pretty-blue-screen/
  24. 24. Basic debugging techniques
  25. 25. var_dump • Tracking variables throughout code • Add formatting: • Better: Zend_Debug::dump()
  26. 26. Debug window • Javascript debug window • http://devzone.zend.com/article/1256
  27. 27. Firebug console logging • Firebug and FirePHP plugins for Firefox • Doesn’t send output to HTML page so won’t interfere with sessions, headers, etc • Zend_Log_Writer_Firebug • www.getfirebug.com
  28. 28. ZFDebug • Zend Framework plugin to display debug information (time spent, memory usage, database queries, etc) • http://jokke.dk/software/zfdebug
  29. 29. A proper debugger • Step through your code line by line • Set breakpoints • View environment, script variables and function paramters at each step • View browser output • Profiling
  30. 30. Xdebug • Open Source • Packaged with Komodo IDE • Or via http://www.xdebug.org/
  31. 31. Zend Debugger • Commercial • Part of Zend Studio • Debugging from Firefox or from within Zend Studio
  32. 32. Best practises • Use an IDE with PHP syntax checking • Enforce coding standards • Comment your code! (PHPDoc) • Defensive programming • Lightweight inital PHP file to help report errors • Break complex code into smaller chunks (OO) • Peer review • Write Unit tests for small, discrete bits of code • Ensure the staging site is on the same server setup as the live site
  33. 33. Links • Zend Studio - www.zend.com/studio • Eclipse + PDT - www.zend.com/en/community/pdt • Komodo - www.activestate.com/komodo/ • Zend Framework docs - http://framework.zend.com/ manual/en/ • Blog article: Debugging development - http://techportal.ibuildings.com/2009/03/16/debugging- development/
  34. 34. Thanks! Simon R Jones, Studio 24, www.studio24.net
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×