The Strange World of PHP Debugging
-
date post
21-Oct-2014 -
Category
Technology
-
view
9.503 -
download
3
description
Transcript of The Strange World of PHP Debugging
![Page 1: The Strange World of PHP Debugging](https://reader038.fdocuments.in/reader038/viewer/2022110113/544598b1b1af9fd7068b45cd/html5/thumbnails/1.jpg)
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](https://reader038.fdocuments.in/reader038/viewer/2022110113/544598b1b1af9fd7068b45cd/html5/thumbnails/2.jpg)
White screen of death
We’re trying to avoid this
![Page 3: The Strange World of PHP Debugging](https://reader038.fdocuments.in/reader038/viewer/2022110113/544598b1b1af9fd7068b45cd/html5/thumbnails/3.jpg)
http://www.flickr.com/photos/nebarnix/320901099/
Which makes us feel like this
![Page 4: The Strange World of PHP Debugging](https://reader038.fdocuments.in/reader038/viewer/2022110113/544598b1b1af9fd7068b45cd/html5/thumbnails/4.jpg)
http://www.flickr.com/photos/sjaek/468245227/
While we’d rather be happilygetting on with development
![Page 5: The Strange World of PHP Debugging](https://reader038.fdocuments.in/reader038/viewer/2022110113/544598b1b1af9fd7068b45cd/html5/thumbnails/5.jpg)
• 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](https://reader038.fdocuments.in/reader038/viewer/2022110113/544598b1b1af9fd7068b45cd/html5/thumbnails/6.jpg)
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](https://reader038.fdocuments.in/reader038/viewer/2022110113/544598b1b1af9fd7068b45cd/html5/thumbnails/7.jpg)
Common errors
![Page 8: The Strange World of PHP Debugging](https://reader038.fdocuments.in/reader038/viewer/2022110113/544598b1b1af9fd7068b45cd/html5/thumbnails/8.jpg)
Syntax errors
• Something’s usually in the wrong place..
• T_ENCAPSED_AND_WHITESPACE
![Page 9: The Strange World of PHP Debugging](https://reader038.fdocuments.in/reader038/viewer/2022110113/544598b1b1af9fd7068b45cd/html5/thumbnails/9.jpg)
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](https://reader038.fdocuments.in/reader038/viewer/2022110113/544598b1b1af9fd7068b45cd/html5/thumbnails/10.jpg)
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](https://reader038.fdocuments.in/reader038/viewer/2022110113/544598b1b1af9fd7068b45cd/html5/thumbnails/11.jpg)
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](https://reader038.fdocuments.in/reader038/viewer/2022110113/544598b1b1af9fd7068b45cd/html5/thumbnails/12.jpg)
Error Reporting
![Page 13: The Strange World of PHP Debugging](https://reader038.fdocuments.in/reader038/viewer/2022110113/544598b1b1af9fd7068b45cd/html5/thumbnails/13.jpg)
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](https://reader038.fdocuments.in/reader038/viewer/2022110113/544598b1b1af9fd7068b45cd/html5/thumbnails/14.jpg)
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](https://reader038.fdocuments.in/reader038/viewer/2022110113/544598b1b1af9fd7068b45cd/html5/thumbnails/15.jpg)
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](https://reader038.fdocuments.in/reader038/viewer/2022110113/544598b1b1af9fd7068b45cd/html5/thumbnails/16.jpg)
Error types
• Parse error
• Fatal error
• Warning
• Notice
• Strict notice
![Page 17: The Strange World of PHP Debugging](https://reader038.fdocuments.in/reader038/viewer/2022110113/544598b1b1af9fd7068b45cd/html5/thumbnails/17.jpg)
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](https://reader038.fdocuments.in/reader038/viewer/2022110113/544598b1b1af9fd7068b45cd/html5/thumbnails/18.jpg)
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](https://reader038.fdocuments.in/reader038/viewer/2022110113/544598b1b1af9fd7068b45cd/html5/thumbnails/19.jpg)
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](https://reader038.fdocuments.in/reader038/viewer/2022110113/544598b1b1af9fd7068b45cd/html5/thumbnails/20.jpg)
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](https://reader038.fdocuments.in/reader038/viewer/2022110113/544598b1b1af9fd7068b45cd/html5/thumbnails/21.jpg)
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](https://reader038.fdocuments.in/reader038/viewer/2022110113/544598b1b1af9fd7068b45cd/html5/thumbnails/22.jpg)
![Page 23: The Strange World of PHP Debugging](https://reader038.fdocuments.in/reader038/viewer/2022110113/544598b1b1af9fd7068b45cd/html5/thumbnails/23.jpg)
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 24: The Strange World of PHP Debugging](https://reader038.fdocuments.in/reader038/viewer/2022110113/544598b1b1af9fd7068b45cd/html5/thumbnails/24.jpg)
Pretty Blue Screen
• Nice exception handler from Harry Fuecks
• http://www.sitepoint.com/blogs/2006/04/04/pretty-blue-screen/
![Page 25: The Strange World of PHP Debugging](https://reader038.fdocuments.in/reader038/viewer/2022110113/544598b1b1af9fd7068b45cd/html5/thumbnails/25.jpg)
![Page 26: The Strange World of PHP Debugging](https://reader038.fdocuments.in/reader038/viewer/2022110113/544598b1b1af9fd7068b45cd/html5/thumbnails/26.jpg)
Basic debugging techniques
![Page 27: The Strange World of PHP Debugging](https://reader038.fdocuments.in/reader038/viewer/2022110113/544598b1b1af9fd7068b45cd/html5/thumbnails/27.jpg)
var_dump
• Tracking variables throughout code
• Add formatting:
• Better: Zend_Debug::dump()
![Page 28: The Strange World of PHP Debugging](https://reader038.fdocuments.in/reader038/viewer/2022110113/544598b1b1af9fd7068b45cd/html5/thumbnails/28.jpg)
Debug window
• Javascript debug window
• http://devzone.zend.com/article/1256
![Page 29: The Strange World of PHP Debugging](https://reader038.fdocuments.in/reader038/viewer/2022110113/544598b1b1af9fd7068b45cd/html5/thumbnails/29.jpg)
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](https://reader038.fdocuments.in/reader038/viewer/2022110113/544598b1b1af9fd7068b45cd/html5/thumbnails/30.jpg)
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](https://reader038.fdocuments.in/reader038/viewer/2022110113/544598b1b1af9fd7068b45cd/html5/thumbnails/31.jpg)
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](https://reader038.fdocuments.in/reader038/viewer/2022110113/544598b1b1af9fd7068b45cd/html5/thumbnails/32.jpg)
Xdebug
• Open Source
• Packaged with Komodo IDE
• Or via http://www.xdebug.org/
![Page 33: The Strange World of PHP Debugging](https://reader038.fdocuments.in/reader038/viewer/2022110113/544598b1b1af9fd7068b45cd/html5/thumbnails/33.jpg)
Zend Debugger
• Commercial
• Part of Zend Studio
• Debugging from Firefox or from within Zend Studio
![Page 34: The Strange World of PHP Debugging](https://reader038.fdocuments.in/reader038/viewer/2022110113/544598b1b1af9fd7068b45cd/html5/thumbnails/34.jpg)
![Page 35: The Strange World of PHP Debugging](https://reader038.fdocuments.in/reader038/viewer/2022110113/544598b1b1af9fd7068b45cd/html5/thumbnails/35.jpg)
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 36: The Strange World of PHP Debugging](https://reader038.fdocuments.in/reader038/viewer/2022110113/544598b1b1af9fd7068b45cd/html5/thumbnails/36.jpg)
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/