Categories
Posts

PHP Helpers: html_var_dump

With the last function being html_print_r this next one should come as no suprise, html_var_dump:

[sourcecode lang=”php”]
if ( !function_exists( ‘html_var_dump’ ) ) {
function html_var_dump( $data ) {
$out = "n<pre class=’html-var-dump’";
$out .= " style=’border: 1px solid #ccc; padding: 7px;’>n";

ob_start( );
var_dump( $data );
$out .= esc_html( ob_get_contents( ) );
ob_end_clean( );

$out .= "n</pre>n";

return $out;
}
}
[/sourcecode]

Even though I tend to use print_r more often than var_dump, there are times where the type data that var_dump provides can be very helpful. Unfortunately var_dump will always display data, so it needed to be wrapped in a few output control functions ( ob_start, ob_get_contents, and ob_end_clean ) to capture it.