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

Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 

Último (20)

Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 

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