SlideShare una empresa de Scribd logo
1 de 37
Descargar para leer sin conexión
The Strange World of
  Debugging PHP
    Refresh Cambridge, 1 July 2009

       Simon R Jones, Studio 24
          www.studio24.net
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 PHP debugger

•   Best practises
What is a bug?

•   Human error (typos)

•   Logic errors

•   Environmental errors (files, web service, db)

•   And rarely, it’s a system bug (PHP, Apache)!
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 in Hebrew)
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 to the browser

•   Don’t include the closing ?> tag
    (unless you’re outputting HTML)
Error Reporting
Recommended settings
        Production (live) site
  •   Hide on screen
  •   Display friendly error page
  •   Log errors


          Development site
  •   Display on screen
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
Bitwise operators


• Report all errors..
• But not notices
• And not warnings
• http://php.net/manual/en/function.error-
  reporting.php
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 gotcha: this overrides php.ini error_log
  setting which can be confusing
• Can be overridden with PHP options
  log_errors and error_log
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
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
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);
Watch out!
Errors suppressed with the @
operator are still sent to a
custom error handler. Can cause
headaches and unexplained bugs!
Exceptions


• PHP 5 only
• Only classes can throw an exception
• set_exception_handler() to deal with
  uncaught exceptions
• Uncaught exceptions raise a Fatal error
Pretty Blue Screen




• Nice exception handler from Harry Fuecks
• http://www.sitepoint.com/blogs/2006/04/04/
  pretty-blue-screen/
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 interfere with sessions, headers, etc
• Zend_Log_Writer_Firebug
• www.getfirebug.com
ZFDebug


• Zend Framework plugin to display debug
  information (time spent, memory usage,
  database queries, etc)
• http://jokke.dk/software/zfdebug
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
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)

•   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
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/
Thanks!


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

Más contenido relacionado

Último

Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 

Último (20)

Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 

Destacado

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Destacado (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

The Strange World of PHP Debugging

  • 1. The Strange World of Debugging PHP Refresh Cambridge, 1 July 2009 Simon R Jones, Studio 24 www.studio24.net
  • 2. We’re trying to avoid this White screen of death
  • 3. Which makes us feel like this http://www.flickr.com/photos/nebarnix/320901099/
  • 4. While we’d rather be happily getting on with development http://www.flickr.com/photos/sjaek/468245227/
  • 5. Debugging PHP • What is a bug? • Common errors • Error reporting • Basic debugging techniques • A proper PHP debugger • Best practises
  • 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)!
  • 8. Syntax errors • Something’s usually in the wrong place.. • T_ENCAPSED_AND_WHITESPACE
  • 9. Syntax errors • T_NUM_STRING • T_OBJECT_OPERATOR • T_STRING • T_VARIABLE • T_PAAMAYIM_NEKUDOTAYIM ! (means double colon in Hebrew)
  • 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
  • 16. Error types • Parse error • Fatal error • Warning • Notice • Strict notice
  • 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/
  • 25.
  • 26. Basic debugging techniques
  • 27. var_dump • Tracking variables throughout code • Add formatting: • Better: Zend_Debug::dump()
  • 28. Debug window • Javascript debug window • http://devzone.zend.com/article/1256
  • 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
  • 32. Xdebug • Open Source • Packaged with Komodo IDE • Or via http://www.xdebug.org/
  • 33. Zend Debugger • Commercial • Part of Zend Studio • Debugging from Firefox or from within Zend Studio
  • 34.
  • 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
  • 36. 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/
  • 37. Thanks! Simon R Jones, Studio 24, www.studio24.net