note 9570 deleted from function.var-dump by jimw

From: Date: Sat, 08 Dec 2001 23:58:21 +0000
Subject: note 9570 deleted from function.var-dump by jimw
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-22399@lists.php.net to get a copy of this message
My only complaint with var_dump is that it dumps strings as is which means I can't dump them to a web page if they contain HTML. So I wrote a replacement below: function mydump1 ($thing, $maxdepth, $depth) { $fmt = sprintf ("%%%ds", 4*$depth); $pfx = sprintf ($fmt, ""); $type = gettype($thing); if ($type == 'array') { $n = sizeof($thing); echo "$pfx<B>array($n) =&gt;</B><BR />"; foreach (array_keys($thing) as $key) { echo " $pfx<B>[$key] =&gt;</B>\n"; mydump1 ($thing[$key], $maxdepth, $depth+1); } } else if ($type == 'string') { $n = strlen($thing); echo "$pfx<B>string($n) =&gt;</B>\n"; echo "$pfx\"" . htmlentities($thing) . "\"\n"; } else if ($type == 'object') { $name = get_class($thing); echo "$pfx<B>object($name) =&gt;</B>\n"; $methodArray = get_class_methods($name); foreach (array_keys($methodArray) as $m) { echo " $pfx<B>method($m) =&gt;</B> $methodArray[$m]\n"; } $classVars = get_class_vars($name); foreach (array_keys($classVars) as $v) { echo " $pfx<B>default =&gt; $v =&gt;</B>\n"; mydump1($classVars[$v], $maxdepth, $depth+2); } $objectVars = get_object_vars($thing); foreach (array_keys($objectVars) as $v) { echo " $pfx<B>$v =&gt;</B>\n"; mydump1($objectVars[$v], $maxdepth, $depth+2); } } else if ($type == 'boolean') { if ($type) { echo "$pfx<B>boolean(true)</B><BR />"; } else { echo "$pfx<B>boolean(false)</B><BR />"; } } else { echo "$pfx<B>$type($thing)</B><BR />"; } } function mydump ($thing, $maxdepth=-1) { echo "<PRE>\n"; mydump1 ($thing, $maxdepth, 0); echo "</PRE>\n"; }

« previous php.notes (#22399) next »