note 21036 deleted from function.print-r by betz

From: Date: Sun, 18 Jan 2004 15:38:50 +0000
Subject: note 21036 deleted from function.print-r by betz
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-63786@lists.php.net to get a copy of this message
Note Submitter: garyb@fxt.com ---- 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

« previous php.notes (#63786) next »