PHP – printing arrays to your screen with print_r

Posted by on Jul 03 2009, in php, print_r, zegarkus

this from originally from http://zegarkus.com/2006/09/24/a-handy-print_r-function/


 

print_r() is a handy function to print items in array to your screen. Its a great way to test and make sure your arrays have all the information they need. However its display can get quite messy if you have mulit-dimensional arrays. For example:

php print_r

The output would look like this:

 

php print_r

 

It shows all the information, however its bit difficult to see what is going on in there, all the arrays are lumped together. A better way of displaying the data would be to add some old school HTML tags such as the pre tags:

 

php print_rNow the out put looks like this:

php print_r

This is alot easier to read as it stacks the arrays nicely. Finally you just chuck it into a handy function. I use this function to test data, check for errors and much more:

 

php print_r