- Word of the day: Ouch.
- I’m done with the cancer, but the cancer isn’t done with me
- Goodbye, my dopey dog.
- So long, Alex King
Polluting the internet since 2004
September 27, 2011 | Web Design & Development
I’ve come to be pretty reliant on a few little dev helper functions that I’ve written that help introspect data in a couple of different output methods. A lot of development, especially development with unfamiliar systems, involves looking at objects and their contents.
These helpers are designed to help give consistently formatted output no matter where they’re used. They output specific styling inline to override the current site’s style sheets and output readable, monospace formatted code in most scenarios.
dp() functions output to screen and the
ep() function outputs to the error log.
Copy the code above, or copy from the Gist File. Paste the contents in to a file that is accessible to your web-server software. Next, edit your
php.ini file and edit the line with the
auto_prepend_file setting with the full path to your helper file. Now restart apache to reload the
All methods will output simple scalar values in their normal format and will translate boolean values to their string values instead of their numerical values. Object and arrays are then run through either
var_dump() depending upon which method you use.
pp() will give you the most readable output as it uses
print_r() to output. But one drawback of
print_r() is that it doesn’t tell you what type the variable is. This can be a drawback if you’re working specifically against boolean or null types. In that case you can use the
dp() method which uses
var_dump() to output.
var_dump()‘s output is less easy to read, but it does output the variable types which is very handy.
ep() method outputs to the PHP error log. Handy for inspecting data in ajax or api calls or when outputting data is either inconvenient or hard to access.
All functions can be overloaded to take as many arguments as required. Each parameter is parsed separately and output comma separated.
There’s only one: make sure that you don’t commit code that contains these function calls – others likely won’t have the same helper methods and your production server most assuredly doesn’t. There’s nothing like committing these methods and not realizing it until someone asks you why ‘dp() is undefined’. I’m actually pretty close to putting pre-commit hooks in to my version control repositories that reject any commit that has these methods in it. Its very easy to forget to strip them out first.
So, there it is. Nothing special, but I find these lil’ guys super handy. I hope you do too.
Update 2012-02-10: Updated the
ep() function to output each line of a multi-line log message to its own line.
Update 2012-05-27: added
htmlspecialchars to output for
dp() functions to fix issue where html would render instead of be shown in its helpful, readable source.
Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.
I’ve gone ahead and replaced this with my simple prp function that simply wraps a print_r function.
BTW: I forked your gist since whitespace isn’t a CSS property, even though it might work.
Ack! Not sure why whitespace works (it probably doesn’t, I’ve probably just not run in to anything that overrides the white-space properties of a pre element).
I’ll get that fixed.
Comments are closed