Fork me on GitHub

article

A few PHP Dev Helper functions

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.

The pp() & dp() functions output to screen and the ep() function outputs to the error log.

  1.  <?php
  2.  /**
  3.   * A few helper functions for debugging PHP
  4.   * See: http://top-frog.com/2011/09/27/a-few-php-dev-helper-functions/ for info
  5.   */
  6.  $__style = 'white-space: pre; text-align: left; '.
  7.   'font: normal normal 11px/1.4 menlo, monaco, monospaced; '.
  8.   'background: white; color: black; padding: 5px; '.
  9.   'letter-spacing: normal; word-spacing: normal';
  10.  function pp() {
  11.   global $__style;
  12.   $msg = __v_build_message(func_get_args());
  13.   echo '<pre style="'.$__style.'">'.htmlspecialchars($msg).'</pre>';
  14.  }
  15.  function dp() {
  16.   global $__style;
  17.   $msg = __v_build_message(func_get_args(), 'var_dump');
  18.   echo '<pre style="'.$__style.'">'.htmlspecialchars($msg).'</pre>';
  19.  }
  20.  function ep() {
  21.   $msg = __v_build_message(func_get_args());
  22.   $msg_array = explode("\n", $msg);
  23.   foreach ($msg_array as $line) {
  24.   error_log('**: '.str_replace("\t", ' ', $line));
  25.   }
  26.  }
  27.  function __v_build_message($vars, $func = 'print_r', $sep = ', ') {
  28.   $msgs = array();
  29.   if (!empty($vars)) {
  30.   foreach ($vars as $var) {
  31.   if (is_bool($var)) {
  32.   $msgs[] = ($var ? 'true' : 'false');
  33.   }
  34.   elseif (is_scalar($var)) {
  35.   $msgs[] = $var;
  36.   }
  37.   else {
  38.   switch ($func) {
  39.   case 'print_r':
  40.   case 'var_export':
  41.   $msgs[] = $func($var, true);
  42.   break;
  43.   case 'var_dump':
  44.   ob_start();
  45.   var_dump($var);
  46.   $msgs[] = ob_get_clean();
  47.   break;
  48.   }
  49.   }
  50.   }
  51.   }
  52.   return implode($sep, $msgs);
  53.  }
  54.  ?>

Install

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 php.ini.

Usage

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 print_r() or var_dump() depending upon which method you use.

In general 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.

The 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.

Caveat

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 ep() and dp() functions to fix issue where html would render instead of be shown in its helpful, readable source.

2 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  • Excellent.

    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.

    Dan, September 27, 2011 11:13 am | permalink

  • 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.

    Shawn, September 27, 2011 3:20 pm | permalink

Comments are closed