SlideShare uma empresa Scribd logo
1 de 37
Baixar para ler offline
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

Mais conteúdo relacionado

Último

Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
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
 
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
 
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
 
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
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
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
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
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
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 

Último (20)

Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
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
 
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
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
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
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
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)
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
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
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 

Destaque

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
 

Destaque (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