10. Whose line is it anyway?
• The syntax error isn’t always on the line
reported
• Usually the case with missing quotes
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)
13. Recommended settings
Production (live) site
• Hide on screen
• Display friendly error page
• Log errors
Development site
• Display on screen
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. Bitwise operators
• Report all errors..
• But not notices
• And not warnings
• http://php.net/manual/en/function.error-
reporting.php
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. 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. 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. 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. Watch out!
Errors suppressed with the @
operator are still sent to a
custom error handler. Can cause
headaches and unexplained bugs!
22.
23. Exceptions
• PHP 5 only
• Only classes can throw an exception
• set_exception_handler() to deal with
uncaught exceptions
• Uncaught exceptions raise a Fatal error
24. Pretty Blue Screen
• Nice exception handler from Harry Fuecks
• http://www.sitepoint.com/blogs/2006/04/04/
pretty-blue-screen/
29. 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
30. ZFDebug
• Zend Framework plugin to display debug
information (time spent, memory usage,
database queries, etc)
• http://jokke.dk/software/zfdebug
31. 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
35. 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