
When you are using a print_r in php, it’s a very good idea to remember the print_r pre combo (ie wrap the print_r in a pre tag). The difference being that using pre will allow you to understand the output as it will be preformatted in a way that is easy for humans to see what is going on.
The Code
Suppose you start off with an array as follows
1 2 3 4 5 6 7 |
<?php $array = array ( 'group1' => 'josh', 'group2' => 'steve', 'group3' => array ('tom', 'sam', 'david') ); ?> |
If you use print_r without wrapping in a pre tag, for example
1 |
<?php print_r ($array); ?> |
Your output will look like this
1 |
Array ( [group1] => josh [group2] => steve [group3] => Array ( [0] => tom [1] => sam [2] => david ) ) |
That’s hard to read. However if you wrap the print_r in a pre tag, like so
1 2 3 |
<pre> <?php print_r ($array); ?> </pre> |
Your output will look like this
1 2 3 4 5 6 7 8 9 10 11 12 |
Array ( [group1] => josh [group2] => steve [group3] => Array ( [0] => tom [1] => sam [2] => david ) ) |
Much better. I hope it comes in handy and would love to hear from you if it does.