Debugging in PHP

The art of debugging code requires a good understanding of the code base and logical deduction skills. A good debugging tool is like a code mentor that can walk you through the code and help you better understand what is going on, but the deductive reasoning is still up to you. In this session, you will learn how to use Xdebug, a PHP debugging tool to:

  • Step through the code line-by-line
  • Set break points to pause the code at any point
  • View the variables that are in scope when the code is paused
  • Watch variables and functions as you step through the code
  • Change variable values as the code runs
  • Investigate the call stack to see the files loaded and functions run
  • Surface hidden issues in your code

Setting up Debugging with VVV and PHPStorm

  • Install and setup VVV
  • SSH into VVV with vagrant ssh and run the xdebug_on command
  • Install PHPStorm
  • Setup PHPStorm debugging bookmarklets
  • Open a VVV site / project in PHPStorm
  • Click the ‘Listen for Debugger Connections’ (phone icon) in PHPStorm
  • Set a break point somewhere in your PHPStorm project
  • In your browser, click the ‘Start Debugger’ bookmarklet
  • Load the site in your browser.

Debugging Steps

  • Identify the Issue
    • Steps to Reproduce
    • Expected Results
    • Actual Results
    • Workarounds
    • Error Messages
    • Environmental Information
  • Duplicate the Issue
  • Isolate the Issue
    • Determine the type of issue
      • Visual (HTML/CSS)
      • Interactive (JS)
      • Functionality (PHP)
      • Data (MySQL)
      • Environmental (Server)
      • Performance
      • Security
      • Caching
    • Determine the source of issue
      • WordPress core
      • Theme
      • Plugin
      • Data
      • Web Host / Server
      • Third Party API
    • Methodology
      • Process of elimination
      • Interactive debugging
      • Unit Tests
  • Fix the Issue
    • Prevent collateral damage (Single Responsibility Principle)
    • Setup a regression test
  • Test the Issue

Debugging Resources


 

Presented at WordCamp Asheville 2015 and WordCamp Tampa 2016

Advertisements