We’re working on a lot of projects at the moment that require tons of data being shifted around, usually asynchronously. During the development stage its sometimes useful to print all your available data to the screen so you can see if anything is being sent over incorrectly (or not at all). The usual print_r function dumps the output of an array to the screen in a giant block with no linebreaks or intentation, making it difficult to scan read.

After a quick browse i came across a few neat functions which will allow a more human readable version to be printed to the screen, and after some small modifications to the syntax, we arrived at this…

function print_r_html($data)
{
	echo '< pre >' . print_r($data, TRUE) . '< / pre >';
}

I had to put some spaces in my pre tags because the syntax highlighter didnt like it, so make sure you take those out

Slip this into your common/misc functions file (we use a functions file specifically for debugging during development so we can activate/deactivate it for use in a live environment) and then simply call it with

print_r_nice($data)

Some examples i found had checks in to make sure the variable being passed was an array but thats almost always redundant for our purposes, so out it goes. In case anyone was wondering the ‘TRUE’ just prevents the output of print_r from being returned before we’ve had a chance to do anything with it.