note 41588 deleted from function.var-dump by tomsommer

From: Date: Thu, 15 Apr 2004 17:33:36 +0000
Subject: note 41588 deleted from function.var-dump by tomsommer
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-68173@lists.php.net to get a copy of this message
Note Submitter: egypt@nmt.edu ---- This is a reposting of a function i found on this page about a year ago that (for some reason unbeknownst to me) has subsequently been removed. i've cleaned it up a bit, and i believe it now outputs valid xhtml 1.0 strict. <?php /** * fancy_vardump(): Recursively show information about a variable * * The returned string will contain an html depiction of the variable's * contents. Arrays and objects are displayed recursively. * * @return string Pretty display of the contents of * @param mixed * @link http://www.php.net/var_dump (var_dump manual) */ function fancy_vardump(&$vInput, $depth = 0) { $bgs = array ('#cccccc', '#bde9ff', '#fff1ca', '#c4f0ff'); $bg = &$bgs[$depth % sizeof($bgs)]; $s = '<table border="0" cellpadding="4" cellspacing="1">'. "\n". '<tr><td style="background: none '. $bg . '; text-align: left;'; if (is_int($vInput)) { $s .= '">'. "\n"; $s .= sprintf('int (<b>%d</b>)', intval($vInput)); } else if (is_float($vInput)) { $s .= '">'. "\n"; $s .= sprintf('float (<b>%f</b>)', doubleval($vInput)); } else if (is_string($vInput)) { $s .= '">'. "\n"; $s .= sprintf('string[%d] (<b>"%s"</b>)', strlen($vInput), $vInput); } else if (is_bool($vInput)) { $s .= '">'. "\n"; $s .= sprintf('bool (<b>%s</b>)', ($vInput === true ? 'true' : 'false')); } else if (is_resource($vInput)) { $s .= '">'. "\n"; $s .= sprintf('resource (<b>%s</b>)', get_resource_type($vInput)); } else if (is_null($vInput)) { $s .= '">'. "\n"; $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;">'."\n" . '<table border="0" cellpadding="4" cellspacing="1">'."\n"; 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;">=&gt;</td>'."\n"; $s .= '<td style="background-color: '. $bg . '; text-align: left;">' . "\n" . fancy_vardump($vVal, $depth+1) . '</td>'; $s .= "</tr>\n"; } $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;">=&gt;</td>'."\n"; $s .= "<td style='background-color: $bg; text-align: left;'>\n" . fancy_vardump($vVal, $depth+1) . '</td>'; $s .= "</tr>\n"; } $s .= '</table>'; } else { $s .= '">' . "\n"; $s .= sprintf('<b>unhandled (gettype() reports "%s")', gettype($vInput)); } $s .= "</td></tr></table>\n"; return $s; } // ------------------- // end fancy_vardump() // ------------------- ?>

« previous php.notes (#68173) next »