Debugging PHP code: Tips and Tricks

Share Button

PHP can sometimes be a really hard coding language to work with because when it fails, it fails hard. That is: you typically know something is wrong because the page returns a blank 500 error with no helpful debugging information provided. There are, however, several awesome PHP debugging tricks I have come across in my use of PHP that have been immensely helpful when trying to track down where the error is.

Print Errors instead of 500-ing

You can set headers in your PHP document to display errors when they occur instead of failing. You do this by adding the following script to the top of the php document you want to debug:

Now the page will display information about the error it encountered, and on which line of code the error originated. This is probably the single most helpful PHP debugging tool I have come across, as it completely eliminates the ominous blank 500 error page of death.

var_dump PHP Objects

When working with PHP objects, it is also sometimes hard to know exactly what is going on especially if you are working with dynamic content. One great way to have a deeper look into the current state of your PHP object is to var_dump it to the screen:

var_dump PHP Objects in JSON

While var_dumping PHP object makes it a lot easier to understand what’s going on with your objects, I prefer looking at JSON formatted objects rather than the PHP version. This is a total preference thing, and if you like the way PHP objects read than this may not apply to you. You can convert a PHP object to JSON and then echo it out to the screen like so:

Visualizing JSON

When outputting a JSON object that is really large, it can be pretty hard to read through the data. When dealing with large objects, I typically use the assistance of a JSON formatter & highlighter. There are 2 web-apps in particular that I have found work the best when dealing with JSON:
I have found to be the more useful of these two tools, mostly because it has a better editing interface. It formats the json with basic indentation, so writing in new json variables or copy/pasting back to your document can be easier.

json Viewer
I like using when diving into a JSON objects structure. It has a “tree view” UI that allows a user to expand the JSON object much like you would a folder structure on a conventional Operating System GUI.

json Viewer

These are some of the most helpful PHP debugging tools that I use regularly to work through my PHP issues, but if you have your own workflow that you would like to share, leave a comment below!

Share Button
Riley scribbled this article down on: December 21, 2013
  • Ceceilia Hedrick

    I was wondering what online courses you used if any to lean PHP

  • Lewis Cowles

    This is a great start, but what about in production? Well the best thing about PHP is trigger_error function and setting error_handler, exceptions well as try catch blocks. Using these you can actually send off errors automatically to your project management system etc in whatever format you deem appropriate, this is what any project we code from scratch in PHP does, and allows us to stay way ahead of the curve.

    Something else people may not be aware of is that warnings and notices slow the application down even if they are not reported, so you could have a fair bit more responsiveness from scripts using methods to automatically notify you of errors*.

    * if you fix the errors