By: Chuka Okoye Posted on: 2014-06-28
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.