Make print_r() Pretty
This is literally print_r() dressed up. I normally run this within my general $app object so to call it, I would use $app->debug($var). As with print_r(), it loops indefinitely using lists to format a readable layout in HTML. This helps the developer avoid having to view source and scroll…and scroll…and scroll to find the output of an array or object.
class System_Application {
/* ***********************************************************
* START :::
* Used for writing variables and arrays to screen
* for debugging. If array, it will place a line break
* after each value for easy on-screen viewing
*
* $app->debug($var);
*
* if var is empty, it will display "Hello World"
* $show defaults to true. setting this false will wrap the
* output in a div with a display:none so it won't show
* except if you view HTML source
*********************************************************** */
function debug($var=false,$show=true) {
$display=(!$show)?' style="display: none;"':'';
echo '<div class="light-font"'.$display.'> ';
if ( is_array($var) || is_object($var) ):
foreach ($var as $key=>$val) :
echo $key;
if (is_array($val) || is_object($val)) :
echo '<ul style="margin:0;padding:0 0 0 15px;list-style:none;">';
foreach ($val as $key1=>$val1) :
echo '<li>'.$key . '['.$key1.']' . ' = ';
if (is_array($val1) || is_object($val1)) :
$this->debug_array($val1);
else :
echo $val1.'</li>';
endif;
endforeach;
echo '</ul>';
else :
echo ' = ' . $val.'<br />';
endif;
endforeach;
else:
echo $var;
endif;
if (empty($var)) :
echo 'Hello World ';
endif;
echo '</div>';
}
function debug_array($var) {
echo '<ul style="margin:0;padding:0 0 0 25px;list-style:none;">';
foreach ($var as $key1=>$val1) :
echo '<li>[' . $key1 . '] = ';
if (is_array($val1) || is_object($val1)) :
$this->debug_array($val1);
else :
echo $val1.'</li>';
endif;
endforeach;
echo '</ul>';
}
/* ***********************************************************
* END :::
*********************************************************** */
}
$app = new System_Application;
Use:
/* an example of an icon array I have for twitter app icons */ var $icon = array( 'toolbar' => array( 'logout' => 'hi-res/Logout.png', 'compose' => 'hi-res/Compose.png', 'reload' => 'hi-res/Reload.png', 'scrolltop' => 'hi-res/Scroll-Top-Alt.png' ), 'tweet' => array( 'profile' => 'hi-res/Profile.png', 'reply' => 'hi-res/Reply.png', 'dm' => 'hi-res/DM.png', 'links' => 'hi-res/Links.png', 'favorite' => 'hi-res/Favorite-Alt.png' ) ); $app->debug($icon);
Results:
<p class="light-font">
toolbar<ul style="margin:0;padding:0 0 0 15px;list-style:none;">
<li>toolbar[logout] = hi-res/Logout.png</li>
<li>toolbar[compose] = hi-res/Compose.png</li>
<li>toolbar[reload] = hi-res/Reload.png</li>
<li>toolbar[scrolltop] = hi-res/Scroll-Top-Alt.png</li>
</ul>
tweet<ul style="margin:0;padding:0 0 0 15px;list-style:none;">
<li>tweet[profile] = hi-res/Profile.png</li>
<li>tweet[reply] = hi-res/Reply.png</li>
<li>tweet[dm] = hi-res/DM.png</li>
<li>tweet[links] = hi-res/Links.png</li>
<li>tweet[favorite] = hi-res/Favorite-Alt.png</li>
</ul>
</p>
Which translates to:
toolbar
- toolbar[logout] = hi-res/Logout.png
- toolbar[compose] = hi-res/Compose.png
- toolbar[reload] = hi-res/Reload.png
- toolbar[scrolltop] = hi-res/Scroll-Top-Alt.png
tweet
- tweet[profile] = hi-res/Profile.png
- tweet[reply] = hi-res/Reply.png
- tweet[dm] = hi-res/DM.png
- tweet[links] = hi-res/Links.png
- tweet[favorite] = hi-res/Favorite-Alt.png
There ya go. That’s all there is too it.

This would have been nice when I was doing a lot of poking around CakePHP. Thanks!