Last Updated: February 25, 2016
·
1.552K
· montanaflynn

Prettify print_r output

If you deal with objects or arrays in PHP you probably have heard of print_r, in a nutshell it prints all of an object or array. Trouble is, by default it can look very ugly and isn't super helpful for debugging because html doesn't respect the whitespace. This is exactly what the <pre> tag was meant for and by wrapping it around print_r you get much more readable output. Here's a 3 line function that makes it so.

function pretty_print($arg) {
  echo '<pre>';
  print_r($arg);
  echo '</pre>';
}

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

PHP applications still send a browser full of JavaScript. Hardening the server does not inventory the payment, analytics, and plugin scripts that run after the HTML is delivered.

Security insights from Cside