note 28317 modified in reserved.variables by jmcastagnetto
| From: | jmcastagnetto@php.net | Date: | Thu, 09 Jan 2003 06:11:18 +0000 |
| Subject: | note 28317 modified in reserved.variables by jmcastagnetto | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-41885@lists.php.net to get a copy of this message | ||
[Editor's note: If you are using an older version of PHP in which this (and other bugs) exists,
upgrade as soon as possible. This problem has long been fixed since 4.0.4 was released back in
December 19, 2002, over 2 years ago today (2003/01/08, current release 4.3.0). The version 4.0.2 was
released in August 29, 2002.]
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 (' ', $ident ) :
'';
$out = '';
if (is_array ($var)) {
$out .= "Array (<_br_>\n";
foreach ($var as $key => $val) {
$out .= "$spc " . "[$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 " . "->$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');
?>
--was--
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 (' ', $ident ) :
'';
$out = '';
if (is_array ($var)) {
$out .= "Array (<_br_>\n";
foreach ($var as $key => $val) {
$out .= "$spc " . "[$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 " . "->$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