PHP Function to Wrap print_r in <pre> Preformatted Text Tag

Finally got tired of manually wrapping print_r in <pre> tags so I’m just going to drop this here for future reference:

// wrap print_r in preformatted text tags 
// usage: print_pre($value)
function print_pre($value) { 
    echo "<pre>",print_r($value, true),"</pre>";
}

Basically, using print_pre (if the name doesn’t collide w/ another function in your program) will work just like print_r except of course wrap the output in preformatted text tags <pre>.

I do a lot of work in WordPress so if you can just drop this into your functions.php file if you’d like to use it. The same goes for Drupal, add it to your themes file for easy access. Another option I supposed would be to prepend it to your PHP documents but I’m just speculating right now and that could turn out not so good.

Anyways, print_pre can be used in place of print_r to dump output of print_r pre wrapped in pre tags.

References:

This is probably the better long term solution:

Post written by Ed Reckers

Founder and lead web development consultant at Red Bridge Internet : San Francisco WordPress Developers and Consultants.

One Response to “PHP Function to Wrap print_r in <pre> Preformatted Text Tag”

  1. Conan Blackburn

    A lot of developers use PHP’s print_r() function to display human-readable information about a variable. However, we have to wrap the output in

    
    

    tags otherwise it gets spit out in a jumbled mess and doesn’t display nicely.still Useful, but not that useful.

Leave a Reply