Archive for February, 2010

Utilizing PHP’s print_r() function

This is just a quick tip, but ever since discovering it I use it constantly. A lot of developers use PHP’s print_r() function to display human-readable information about a variable, which makes it great for debugging arrays and objects (among other things) very quickly. However, you have to wrap the output in <pre></pre> tags otherwise it gets spit out in a jumbled mess and doesn’t display nicely.

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

That works just fine, but you can shorten it up by passing true to print_r()’s second parameter, the $return flag, and mixing it in with your echo statements. What this does is tells print_r() to return the human-readable variable data instead of printing it to the screen right away.

echo '<pre>' . print_r($foo, true) . '</pre>';

Earth-shattering code breakthrough? Absolutely (not). However, it does turn three lines of code into one and makes it quicker (for me, anyway) to write quick debugging statements and move them around easier.

As an added bonus, you can “queue up” a bunch of print_r()’s by capturing their output in a variable and then echo’ing it just once.

$debug = print_r($foo, true);
$debug .= print_r($bar, true);
echo '<pre>' . $debug . '</pre>';

Voila, shorter debugging statements that are easier to work with.

Sites Merged, New WordPress Site!

It’s been a few years in the making, but I’ve finally moved my blog from shatteredreality.com to mikebranski.com, and I’m working on updating the photography content that was here originally – for now it’s available at http://mikebranski.com/v1/.

When it came down to it, it was just too much work trying to keep three sites up and active and full of content, which led me to update none of them. Far from ideal. It’s also a lot easier to give people one link instead of three and makes it less confusing when they don’t have to try to decide which one they want to visit.

Next steps will be to take my web development stuff over at Left Right Designs and bring that here as well, which will coincide with me getting some of my projects set up on GitHub. Over the next few days I’ll also clean up my blog categories and tags that came over with my old blog and start filling out some of my pages.

Until then, have a look through some of my other posts while I get settled.

Update: For those of you who subscribed to my old feed, be sure to grab the updated link! The old Feedburner one will still work, but you should update yours anyway.