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.
2 replies on “PHP Helpers: html_var_dump”
Why not just set the return flag for var_export?
$out .= var_export( $data, true );
Or are var_dump and var_export too different for your tastes?
And to aid in having to remember which vars go into which function, I wrap both in another function that chooses for me:
if ((is_string($var) && ! preg_match('/^s*$/', $var))) { // non-whitespace strings
$var = print_r($var, true);
}
else {
if ( ! function_exists('xdebug_disable')) {
if (is_array($var) || is_object($var)) {
$var = print_r($var, true);
}
else {
$var = var_export($var, true);
}
}
else {
$var = var_export($var, true);
}
}
that way, I always know what’s going on with my variables and it’s in the format I like. No confusion between an empty string and FALSE or NULL.
I like that var_dump provides the type and length of each variable. var_export doesn’t provide either of those.