note 28317 added to reserved.variables

From: Date: Thu, 09 Jan 2003 05:34:57 +0000
Subject: note 28317 added to reserved.variables
Groups: php.notes 
Request: Send a blank email to php-notes+get-41881@lists.php.net to get a copy of this message
When using print_r() to see the contents of $GLOBALS, it falls in an infinite loop, because $GLOBALS contains itself. I don't know why, but it's this way. at least with PHP 4.0.2. In order to avoid this infinite loop, I've created a function that recursively prints any kind of variable, stopping if it founds a nested variable with the same name (it's configurable, though). You have to call it with the variable you want to inspect, and (optionally) the name of the variable. When the name is specified, the function looks for loops; when not, it will go on forever. Before using this code, replace every "<_br_>" with "<", "br" and ">", you know ;) . <?php function show_var ($var, $name = '', $ident = 0) { $spc = $ident > 0 ? str_repeat ('&nbsp;&nbsp;&nbsp;', $ident ) : ''; $out = ''; if (is_array ($var)) { $out .= "Array (<_br_>\n"; foreach ($var as $key => $val) { $out .= "$spc&nbsp;&nbsp;" . "[$key] => "; $out .= ($key == $name) ? "LOOP<_br_>\n" : show_var ($val, $name != '' ? $key : '', $ident + 1); } $out .= "$spc)<_br_>\n"; } else if (is_object ($var)) { $clase = get_class ($var); $parent = get_parent_class ($var); $out .= "Object [class $clase"; $out .= $parent != '' ? "extends $parent" : ''; $out .= "] (<_br_>\n"; $arr = get_object_vars ($var); while (list($prop, $val) = each($arr)) { $out .= "$spc&nbsp;&nbsp;" . "->$prop = "; $out .= show_var ($val, $name != '' ? $prop : '', $ident + 1); } $out .= "$spc)<_br_>\n"; } else if (is_resource ($var)) { $out .= "Resource ( " . $var . " )<_br_>\n"; } else if (is_string ($var)) { $out .= '"' . htmlspecialchars($var) . '"' . "<_br_>\n"; } else if (is_numeric ($var)) { $out .= $var . "<_br_>\n"; } else if (is_bool ($var)) { $out .= ($var ? 'True' : 'False') . "<_br_>\n"; } else if (is_null ($var)) { $out .= "Null<_br_>\n"; } else { $out .= "Other ( " . $var . " )<_br_>\n"; } return $out; } echo show_var ($GLOBALS, 'GLOBALS'); ?> -- http://www.php.net/manual/en/reserved.variables.php http://master.php.net/manage/user-notes.php?action=edit+28317 http://master.php.net/manage/user-notes.php?action=delete+28317 http://master.php.net/manage/user-notes.php?action=reject+28317

« previous php.notes (#41881) next »