PHP Debug Function
14
Here's a basic function for debugging any kind of PHP variable. It allows for "invisible" or "visible" output--that is output in HTML comments or within visible HTML tags. It also supports customizable "containers" which lets you easily edit the look of the visible and invisible blocks. Examples in code below.
<?php
/**
* Simple Debug Function
*
* @param mixed $var
* @param boolean $visible
* @param boolean $return
* @return mixed
*/
function varDebug(&$var, $visible = true, $return = false)
{
$containers = array(
// $visible == false
0 => array(
'head' => "\n<!--\n\nDEBUG:\n---------------------------------------\n\n",
'foot' => "\n\n---------------------------------------\n\n-->\n"
),
// $visible == true
1 => array(
'head' => '<hr /><h1>Debug</h1><p>',
'foot' => '</p><hr />'
)
);
$r = var_export($var, true);
if ($visible) $r = str_replace(array(' ', "\n"), array(' ', "<br />\n"), $r);
$container = intval($visible);
$r = $containers[$container]['head'] . $r . $containers[$container]['foot'];
if ($return) return $r;
echo $r;
}
/**
* EXAMPLES:
*/
$a = array('h' => 1, 'e' => 2);
varDebug($a);
/*
OUTPUTS:
<hr /><h1>Debug</h1><p>array (<br />
'h' => 1,<br />
'e' => 2,<br />
)</p><hr />
*/
varDebug($a, false);
/*
OUTPUTS:
<!--
DEBUG:
---------------------------------------
array (
'h' => 1,
'e' => 2,
)
---------------------------------------
-->
*/
varDebug($a, false, true);
/*
OUTPUTS nothing
*/
?>






I've got good news, and I've got bad news:
The universe is merely a figment of my imagination.
Now are you ready for the bad news?
echo str_replace(array(' ', "\n"), array(' ', "<br />\n"), var_export(${varname}, true));