All posts tagged development

more readable method to show print_r output

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…

1
2
3
4
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

1
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.

programmer joke

A man flying in a hot air balloon suddenly realizes he’s lost. He reduces height and spots a man down below. He lowers the balloon further and shouts to get directions, “Excuse me, can you tell me where I am?”

The man below says: “Yes, you’re in a hot air balloon, hovering 30 feet above this field.”

“You must work in Information Technology,” says the balloonist.

“I do” replies the man. “How did you know?”

“Well,” says the balloonist, “everything you have told me is technically correct, but It’s of no use to anyone.”

The man below replies, “You must work in management.”

“I do” replies the balloonist, “But how’d you know?”

“Well”, says the man, “you don’t know where you are, or where you’re going, and you expect me to be able to help. You’re in exactly the same position you were before we met, but now it’s my fault.”

You can find us on Twitter & Facebook