Print_r: A Very Useful PHP Function

Have you ever had an array or object returned by a PHP function, but you didn’t know how it was structured? In order to proceed writing your code, you would need to known what’s in the array, and how to access it. This somes up if you use XML-based APIs a lot, or if you’re dealing with WordPress’s $wpdb class.

That’s where the useful print_r function comes in.

Print_r is implemented simply by calling it, and passing a variable as an argument. The variable you should pass would be your array or object. Also, you need to surround it in <pre> tags (unless you want a jumbled mess that will be hard to read).

echo '<pre>';
print_r($the_variable);
echo '</pre>';

Simple enough, and insanely useful.