note 21036 added to function.print-r
| From: | garyb at fxt dot com | Date: | Fri, 26 Apr 2002 05:19:45 +0000 |
| Subject: | note 21036 added to function.print-r | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-29863@lists.php.net to get a copy of this message | ||
I have used a little recursive function to display arrays and objects for a long time, and I like
its compact format better than print_r() especially for large complex variables. I use it in a
debugging context so it has the ability to print to a file as well. I won't include the setup
stuff - it's available if anyone wants it. Interestingly, if you try it on $GLOBALS it
recognizes functions as objects but there's nothing accessible inside them.
function pray ($data, $functions=0) {
// pray - short for print_array
// Traverse the argument array/object recursively and print an ordered list.
// Optionally show function names (in an object)
// NB: This is a *** HUGE SECURITY HOLE *** in the wrong hands!!
// It prints all the variables it can find
// If the argument is $GLOBALS this will include your database connection information, magic
keys and session data!!
if($functions!=0) { $sf=1; } else { $sf=0 ;} // This kluge seemed necessary on one server.
if (isset ($data)) {
if (is_array($data) || is_object($data)) {
if (count ($data)) {
echo "<OL>\n";
while (list ($key,$value) = each ($data)) {
$type=gettype($value);
if ($type=="array" || $type == "object") {
printf ("<li>(%s) <b>%s</b>:\n",$type, $key);
pray ($value,$sf);
} elseif (eregi ("function", $type)) {
if ($sf) {
printf ("<li>(%s) <b>%s</b>
</LI>\n",$type, $key, $value);
// There doesn't seem to be anything traversable inside functions.
}
} else {
if (!$value) { $value="(none)"; }
printf ("<li>(%s) <b>%s</b> =
%s</LI>\n",$type, $key, $value);
} }
echo "</OL>end.\n";
} else {
echo "(empty)";
} } }
} // function
--
http://www.php.net/manual/en/function.print-r.php
http://master.php.net/manage/user-notes.php?action=edit+21036
http://master.php.net/manage/user-notes.php?action=delete+21036
http://master.php.net/manage/user-notes.php?action=reject+21036