Skip to content

Quick PHP wrapper for print_r

Working on web apps I find myself using the print_r command a lot, and I mean a lot. It helps trace out what data is going where, and more often what’s not going where it’s supposed to.

A few months ago I realized that I type this same bit of code way too often.

echo '<pre>';
print_r($variable);
echo '</pre>';
die();

Does a nice job of wrapping the output in<pre> tags making it easier to trace through.  But too much typing for as many times as I use it.  So I spent 2 minutes kicking out a function named pre_r that does it for me.

/**
* Wrapper for print_r with pre tags
* @param mixed $data   What to display
* @param boolean $die  Whether the function should die() at the end
*/
function pre_r($data, $die=false) {
     echo '<pre>';
     print_r($data);
     echo '</pre>';
     if ($die) {
         die();
     }
}
Published inProgramming

One Comment

  1. DonO DonO

    Smart. I find myself doing this all the time as well. I’m going to add it to my “bag of tricks”.

Leave a Reply

Your email address will not be published. Required fields are marked *