Last Updated: February 25, 2016
·
1.019K
· 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>';
}