Utilizing PHP’s print_r() function

This is just a quick tip, but ever since discovering it I use it constantly. A lot of developers use PHP’s print_r() function to display human-readable information about a variable, which makes it great for debugging arrays and objects (among other things) very quickly. However, you have to wrap the output in <pre></pre> tags otherwise it gets spit out in a jumbled mess and doesn’t display nicely.

echo '<pre>';
print_r($foo);
echo '</pre>';

That works just fine, but you can shorten it up by passing true to print_r()‘s second parameter, the $return flag, and mixing it in with your echo statements. What this does is tells print_r() to return the human-readable variable data instead of printing it to the screen right away.

echo '<pre>' . print_r($foo, true) . '</pre>';

Earth-shattering code breakthrough? Absolutely (not). However, it does turn three lines of code into one and makes it quicker (for me, anyway) to write quick debugging statements and move them around easier.

As an added bonus, you can “queue up” a bunch of print_r()‘s by capturing their output in a variable and then echo’ing it just once.

$debug = print_r($foo, true);
$debug .= print_r($bar, true);
echo '<pre>' . $debug . '</pre>';

Voila, shorter debugging statements that are easier to work with.

Tags:

11 Responses to “Utilizing PHP’s print_r() function”

  1. Jack April 24, 2010 at 12:19 am #

    Good sharing, thanks.

  2. eddy August 20, 2010 at 9:10 am #

    good, but not that much.

  3. Radoslav Stankov September 1, 2010 at 9:42 am #

    Far better idea is to define your own function ( I call it ‘d’ :) ):

    function d(){
    echo ‘

    ';
        foreach(func_get_args() as $v){
           print_r($v);
        }
        echo '

    ‘;
    }

    And you can: d($a, $b, $c);

  4. Spicer Matthews September 9, 2010 at 11:11 am #

    Wow. Sweet. Tip. I use print_r about 100 times a day. Always get annoyed. Particularly with Ajax returns. Tough to do a view source on an ajax return and see the print_r statement.

    Thanks.

  5. AndyH November 4, 2010 at 7:37 am #

    For ajax, modify the above to log to a file and watch that with a separate bit of ajax, or tail -f instead

  6. Will Fastie December 18, 2010 at 9:08 am #

    Very useful tip – I learned something about print_r that I didn’t know. Thanks.

  7. Krrish September 13, 2011 at 7:06 am #

    “queue up” – Really New information for me…

  8. Elvis Zaskarelli September 10, 2012 at 8:09 pm #

    One of the best tips I have come across this year! I got here via a search “print_r in ajax” so thanks very much!

  9. Tamas Kalman October 4, 2012 at 11:21 am #

    printf(‘<pre>%s</pre>’, print_r($variable, true));

Trackbacks/Pingbacks

  1. Building A PHP Database Connection Class From Scratch (using Singleton & ActiveRecord patterns) « CodeSlayer2010 : Slaying Code 24/7! - April 8, 2012

    […] on and you may find it useful debugging tool for your PHP scripts. This class is a wrapper for print_r() function and there are all kinds of advantages, the least of which is one line of code vs three […]

  2. Building A PHP Database Connection Class From Scratch (using Singleton & ActiveRecord patterns) | Wow, I Didn't Know That! - May 17, 2014

    […] on and you may find it useful debugging tool for your PHP scripts. This class is a wrapper for print_r() function and there are all kinds of advantages, the least of which is one line of code vs three […]

Leave a Reply