Call
608.845.2900
Visit
330 Locust Dr.
Verona, WI 53593
Message
info@powderkegwebdesign.com
Powderkeg Web Design
Menu
Message Us
Nick
Nick

Display debug and test information effectively in php

February 13, 2014

If you’ve worked in PHP, you know that debugging things in the middle of website code can be tedious. If you want to echo out a  variable or boolean in a spot that isn’t visually accessible, then you will have a frustrating time viewing the source each page refresh.

A convenient way around this is to create a helper method that returns preformatted text. Ideally you want to define this in a global spot that you can call from anywhere (such as functions.php if you are using WordPress).

Then to call it in our code, we would reference with:

 What this does is it uses php’s “print_r” method that dumps out any information in the variable in a preformatted block that is indented and sectioned off. This goes for strings, objects, arrays, etc. So if I had an array such as:

 Echoing this out will only return “Array”.  Now if you use the new “pk_print” method we created like so:

 The result will be a much better, more informative piece of debug information.

my_arr

It’s a small snippet, but it can save you a lot of time and repeat coding!

Nick
Nick
Senior Developer

Nick is an ambitious and versatile developer with a wide range of talents. He is an efficient coder that utilizes his resources in order to achieve both his and the clients’ goals. His Alma Mater is the University of Wisconsin – Stevens Point where he graduated with honors in Web and Digital Media Development.