37
The Strange World of Debugging PHP Refresh Cambridge, 1 July 2009 Simon R Jones, Studio 24 www.studio24.net

The Strange World of PHP Debugging

  • View
    9.506

  • Download
    3

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: The Strange World of PHP Debugging

The Strange World of Debugging PHP

Refresh Cambridge, 1 July 2009

Simon R Jones, Studio 24www.studio24.net

Page 2: The Strange World of PHP Debugging

White screen of death

We’re trying to avoid this

Page 3: The Strange World of PHP Debugging

http://www.flickr.com/photos/nebarnix/320901099/

Which makes us feel like this

Page 4: The Strange World of PHP Debugging

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

While we’d rather be happilygetting on with development

Page 5: The Strange World of PHP Debugging

• What is a bug?

• Common errors

• Error reporting

• Basic debugging techniques

• A proper PHP debugger

• Best practises

Debugging PHP

Page 6: The Strange World of PHP Debugging

What is a bug?

• Human error (typos)

• Logic errors

• Environmental errors (files, web service, db)

• And rarely, it’s a system bug (PHP, Apache)!

Page 7: The Strange World of PHP Debugging

Common errors

Page 8: The Strange World of PHP Debugging

Syntax errors

• Something’s usually in the wrong place..

• T_ENCAPSED_AND_WHITESPACE

Page 9: The Strange World of PHP Debugging

Syntax errors

• T_NUM_STRING

• T_OBJECT_OPERATOR

• T_STRING

• T_VARIABLE

• T_PAAMAYIM_NEKUDOTAYIM !(means double colon in Hebrew)

Page 10: The Strange World of PHP Debugging

Whose line is it anyway?

• The syntax error isn’t always on the line reported

• Usually the case with missing quotes

Page 11: The Strange World of PHP Debugging

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)

Page 12: The Strange World of PHP Debugging

Error Reporting

Page 13: The Strange World of PHP Debugging

Recommended settings

• Hide on screen

• Display friendly error page

• Log errors

Production (live) site

• Display on screen

Development site

Page 14: The Strange World of PHP Debugging

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

Page 15: The Strange World of PHP Debugging

Bitwise operators

• Report all errors..

• But not notices

• And not warnings

• http://php.net/manual/en/function.error-reporting.php

Page 16: The Strange World of PHP Debugging

Error types

• Parse error

• Fatal error

• Warning

• Notice

• Strict notice

Page 17: The Strange World of PHP Debugging

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

Page 18: The Strange World of PHP Debugging

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

Page 19: The Strange World of PHP Debugging

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

Page 20: The Strange World of PHP Debugging

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);

Page 21: The Strange World of PHP Debugging

Watch out!Errors suppressed with the @ operator are still sent to a custom error handler. Can cause headaches and unexplained bugs!

Page 22: The Strange World of PHP Debugging
Page 23: The Strange World of PHP Debugging

Exceptions

• PHP 5 only

• Only classes can throw an exception

• set_exception_handler() to deal with uncaught exceptions

• Uncaught exceptions raise a Fatal error

Page 25: The Strange World of PHP Debugging
Page 26: The Strange World of PHP Debugging

Basic debugging techniques

Page 27: The Strange World of PHP Debugging

var_dump

• Tracking variables throughout code

• Add formatting:

• Better: Zend_Debug::dump()

Page 28: The Strange World of PHP Debugging

Debug window

• Javascript debug window

• http://devzone.zend.com/article/1256

Page 29: The Strange World of PHP Debugging

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

Page 30: The Strange World of PHP Debugging

ZFDebug

• Zend Framework plugin to display debug information (time spent, memory usage, database queries, etc)

• http://jokke.dk/software/zfdebug

Page 31: The Strange World of PHP Debugging

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

Page 32: The Strange World of PHP Debugging

Xdebug

• Open Source

• Packaged with Komodo IDE

• Or via http://www.xdebug.org/

Page 33: The Strange World of PHP Debugging

Zend Debugger

• Commercial

• Part of Zend Studio

• Debugging from Firefox or from within Zend Studio

Page 34: The Strange World of PHP Debugging
Page 35: The Strange World of PHP Debugging

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

Page 37: The Strange World of PHP Debugging

Thanks!

Simon R Jones, Studio 24, www.studio24.net