Xdebuggin at WordCamp Baltimore 2017

Thanks

Today I had the honor to speak at WordCamp Baltimore on the topic of using Xdebug to write better PHP. Thanks so much to the organizers, volunteers, and attendees for having me. I hope I was able to provide some value.

My slides are here:
https://slides.com/smutek/xdebug/

And notes on my presentation are below.

What is Xdebug

Xdebug is a PHP extension that provides expanded profiling and tracing capabilities beyond those provided natively by PHP.

Simply put, Xdebug is a tool to help us understand our PHP codes. It provides facilities for:

  • Debugging (what went wrong?)
  • Profiling (how efficient is my code?)
  • Tracing (how were things executed?)

What is a bug?

Image of the first bug

We all know what a bug is, but a cool little historical tidbit follows.

On September 9, 1947, engineers found a moth between the relays of the Harvard mark 2 computer they were working on.

One of the engineers, Grace Hopper, taped the moth into the log book with the notation “First actual case of bug being found.” Grace Hopper had just literally debugged the computer.

The logbook (and the bug) is part of the Smithsonian collection.

The engineers did not coin the term “bug”, but it’s widely accepted that it was this incident that made the term stick.

Grade Hoper, by the way, did a lot of awesome things, including earning a PhD in Mathematics from Yale and attaining the rank of Rear Admiral in the US Navy. She was brilliant and amazing.

Grace Murray Hopper
Log Book With Computer Bug | National Museum of American History

 

Why use Xdebug?

Xdebug allows us to view, step through, and manipulate our code while it is being executed.

At the end of the day, on the back end, most of what we are doing involves grabbing some bit of data and displaying it in some way.

Xdebug helps with this by providing insight and clarity into what’s going on with our program as it happens. I sometimes find myself running my debugger when there’s no bugs to debug – I run it simply to see what data is available to me to use.

When not to use Xdebug

Don’t use Xdebug on a public facing server. It is processor intensive and can be a security risk.

The work that Xdebug does is processor intensive and will impact the performance of your production site.

More importantly, Xdebug requires no encryption or authentication by default and provides a ton of sensitive information that could be exploited by malicious users.

It is strictly a development tool.

 

How to get Xdebug?

If you use Vagrant, Xdebug comes preinstalled on Trellis, by the Roots team, and on VVV.

You can spin up your own Vagrant box with Xdebug installed using something like PuPHPet.

Xdebug is also preinstalled on Local, by Flywheel, which is available on OS X and Windows and looks to be a very nice tool.

You can also install it yourself on MAMP, XAMP, etc. or wherever works best for you.

Trellis | WordPress LEMP Stack | Roots
Varying Vagrant Vagrants
Local by Flywheel | Local WordPress development made simple
PuPHPet – Online GUI configurator for Puppet & Vagrant
Xdebug: Documentation

How to use?

There’s tons of ways to run Xdebug. My preference is PhpStorm, but Xdebug clients are also available for:

PhpStorm
PhpStorm: Lightning-Smart IDE for PHP Programming by JetBrains

Sublime Text:
Xdebug – Packages – Package Control

Atom
php-debug

 

See Xdebug: Documentation for a more comprehensive, but not complete, list of Xdebug clients.

 

Browser extensions

I’ve found the following browser extensions to be super helpful, I prefer Xdebug helper.

Xdebug Helper
Firefox: Xdebug Helper for Firefox :: Add-ons for Firefox
Chrome: Xdebug helper – Chrome Web Store

The Easiest Xdebug
Firefox: The easiest Xdebug :: Add-ons for Firefox

Jetbrains Browser Plugins
Xdebug & Zend Debugger Bookmarklets Generator – Marklets | PhpStorm

Project Setup

I’m not going to run through project setup here but I did a post recently on setting Xdebug up in PhpStorm on a Trellis VM.

https://smutek.net/trellis-xdebug-phpstorm/

The principles are the same on any environment, the only variable being the IDE Key. To find out what the IDE key is for your server just setup a phpinfo() file on your server , visit the page, and searxh for xdebug. This process is also covered in the link above.

Note – if you get zero hits when searching xdebug then Xdebug is not installed or configured correctly on your server.

 

Examples

 

Example 1

I was tasked with making a Gravity Form send different emails depending on whether a file was attached or not.

For some reason (as of this writing) Gravity Forms does not include file uploads in conditional logic options.

I used Gravity Forms pre submission filter to accomplish this. Xdebug helped by allowing me to stop code execution and see what sort of variables were available with the $form object before it was submitted to the database.

I found the $_FILES super global, which allowed me to determine whether or not a file was attached.

From there I was able to set up and manipulate a hidden field in my form which could then be targeted by Gravity Forms conditional logic.

The code gist is here:
checkResume.php · GitHub

Info about $_FILES & upload post method.
http://php.net/manual/en/reserved.variables.files.php
http://php.net/manual/en/features.file-upload.post-method.php

Gravity Forms pre submission filter:
gform_pre_submission – Gravity Forms Documentation

 

Example 2

Example 2 was less specific, and simple by intent, but I hoped to illustrate how Xdebug can help to keep track of what’s going on while building complex data structures.

 

Closing

Truthfully, Xdebug has helped me out in so many different ways that it’s nearly impossible to pick which examples to show.

The best thing I can suggest is to give it a try. I’ve found it to be an absolutely indispensable tool and using it has helped me get better at my job.

I hope this has been helpful!

Thanks again to everyone at WordCamp Baltimore. Extra special thanks to my good friend Andrew Adcock for everything you’ve done for me during my journey as a developer.

Leave a Reply

Your email address will not be published. Required fields are marked *