Formatting print_r(); to be readable Published on May 26, 2010
User friendly array listings, by formatting print_r();
When you wish to see the contents of any PHP array, typically you use the print_r() function.
This will print all the array contents in a very messy text block, without any line-breaks whatsoever, wich is quite difficult to read and browse trough.
Wouldn’t it be so much nicer to show the array contents split by line, clean and easy to read and browse., to find what you’re looking after? This is what we will do in this post.
Simply copy and paste this chunk of code in to any .php file:
<?php function print_r_neat($arr) {
?>
<?php print_r($arr); ?>
<?php
} ?>
Now all you have to do is call the function and pass on it the desired array.
Like this:
<?php print_r_neat($YourArray); ?>
For instance:
<?php print_r_neat($_SERVER);?>
And you’ll end up with a nice user friendly array listing!
Have fun!
One Comment on “Formatting print_r(); to be readable”
Go ahead! Leave your comment on this subject!



Have You tried var_dump instead. It will format Your objects/arrays with both line breaks, indent and coloring.