note 33656 added to function.var-dump
| From: | bishop at rack1 dot php dot net | Date: | Thu, 03 Jul 2003 08:05:18 +0000 |
| Subject: | note 33656 added to function.var-dump | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-51399@lists.php.net to get a copy of this message | ||
Adding to the fancy var_dump (originally called test() by rudigier at noxx dot at) and the bug
fixes/enhancements of jounlai at hotmail dot com and rolli at aum dot unibe dot ch, I present an
updated fancy_vardump.
This addresses a few more bugs (original handled neither objects nor resources and failed without
grace on unhandled types), some consistency, and hopefully makes it a little easier to format it to
your own liking:
function fancy_vardump(&$vInput, $depth = 0) {
$bgs = array ('#DDDDDD', '#C4F0FF', '#BDE9FF',
'#FFF1CA');
$bg = &$bgs[$depth % sizeof($bgs)];
$s = "<table border='0' cellpadding='4'
cellspacing='1'><tr><td style='background: none $bg; text-align: left;
";
if (is_int($vInput)) {
$s .= "'>";
$s .= sprintf('int (<b>%d</b>)', intval($vInput));
} else if (is_float($vInput)) {
$s .= "'>";
$s .= sprintf('float (<b>%f</b>)', doubleval($vInput));
} else if (is_string($vInput)) {
$s .= "'>";
$s .= sprintf('string[%d] (<b>"%s"</b>)', strlen($vInput),
$vInput);
} else if (is_bool($vInput)) {
$s .= "'>";
$s .= sprintf('bool (<b>%s</b>)', ($vInput === true ? 'true'
: 'false'));
} else if (is_resource($vInput)) {
$s .= "'>";
$s .= sprintf('resource (<b>%s</b>)', get_resource_type($vInput));
} else if (is_null($vInput)) {
$s .= "'>";
$s .= sprintf('null');
} else if (is_array($vInput)) {
$s .= "border-bottom: solid 2px black;'>";
$s .= sprintf('array[%d]', count($vInput));
$s .= "</td></tr><tr><td style='background: none $bg;
text-align: left;'>" .
"<table border='0' cellpadding='4'
cellspacing='1'>";
foreach ($vInput as $vKey => $vVal) {
$s .= '<tr>';
$s .= "<td style='background-color: $bg; text-align: left;'>"
.
sprintf('<b>%s%s%s</b>', ((is_int($vKey)) ? '' :
'"'), $vKey, ((is_int($vKey)) ? '' : '"')) .
'</td>';
$s .= "<td style='background-color: $bg; text-align:
left;'>=></td>";
$s .= "<td style='background-color: $bg; text-align: left;'>"
.
fancy_vardump($vVal, $depth+1) .
'</td>';
$s .= '</tr>';
}
$s .= '</table>';
} else if (is_object($vInput)) {
$s .= "border-bottom: solid 2px black;'>";
$s .= sprintf('object (<b>%s</b>)', get_class($vInput));
$s .= "</td></tr><tr><td style='background: none $bg;
text-align: left;'>" .
"<table border='0' cellpadding='4'
cellspacing='1'>";
foreach (get_object_vars($vInput) as $vKey => $vVal) {
$s .= '<tr>';
$s .= "<td style='background-color: $bg; text-align: left;'>"
.
sprintf('<b>%s%s%s</b>', ((is_int($vKey)) ? '' :
'"'), $vKey, ((is_int($vKey)) ? '' : '"')) .
'</td>';
$s .= "<td style='background-color: $bg; text-align:
left;'>=></td>";
$s .= "<td style='background-color: $bg; text-align: left;'>"
.
fancy_vardump($vVal, $depth+1) .
'</td>';
$s .= '</tr>';
}
$s .= '</table>';
} else {
$s .= "'>";
$s .= sprintf('<b>unhandled (gettype() reports "%s")',
gettype($vInput));
}
$s .= '</td></tr></table>';
return $s;
}
Call as in:
class Junk {
function Junk() {
$this->a = 1;
$this->b = fopen('http://www.php.net/', 'r');
$this->c = null;
$this->d = array ('first', '2nd' => 'second');
}
}
echo fancy_vardump(new Junk());
Put your colours into the $bgs array in the cycling order you want.
caveat emptor. i'm tired and this Velvet Acid Christ is getting on my nerves.
----
Manual Page -- http://www.php.net/manual/en/function.var-dump.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+33656
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+33656&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+33656&report=yes