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.

BlogBuzz July 26, 2008

WordPress vs. Movable Type: Who’s Winning?

47 Inspiring Web Designs

Google to Buy Digg

GoDaddy Now Registering .me Domains: Does Anyone Care?

Google and Yahoo to Index Flash (Unfortunately)

The PHP Anthology: 101 Essential Tips, Tricks & Hacks [Book Review]

When Should You Upgrade WordPress?

BlogBuzz July 19, 2008