...

View Full Version : $_GLOBALS array and array_walk



StupidRalph
07-08-2007, 08:31 PM
If I used array_walk_recursive on the $GLOBALS array would I have an infinite loop?


ERRORS:
- I initially said array_walk but meant array_walk_recursive.
- I also had $_GLOBALS and of course meant $GLOBALS.

kbluhm
07-09-2007, 08:09 AM
No. $_GLOBALS does not exist.

If you used an array_walk on the $GLOBALS array, same answer.

StupidRalph
07-09-2007, 08:17 AM
Yes I did mean the $GLOBALS array.

The reason that I asked is because I read a book by Rasmus Lerdorf that said print_r($GLOBALS) would be a infinite loop since the $GLOBAL array contains a reference to itself.

kbluhm
07-10-2007, 07:13 AM
You must have read that wrong. I've used print_r( $GLOBALS ) on numerous occasions, and never an infinite loop. I don't believe $GLOBALS references itself. That would be silly.

StupidRalph
07-10-2007, 08:28 AM
Nope. I've read it correctly. However this book might be somewhat dated. Its O'reilly's Programming PHP (http://www.oreilly.com/catalog/progphp/). This book's copyright shows that it was actually published in '02 but it states in chapter 4 the following:

Beware of using print_r() or var_dump() on a recursive structure such as $GLOBALS (which has an entry for GLOBALS that points back to itself). The print_r() function loops infinitely, while var_dump() cuts off after visiting the same element three times. (page. 79).

ralph l mayo
07-10-2007, 09:41 AM
Rasmus' description of var_dump is accurate but print_r has been fixed.



$try_it = array(
'self' => &$try_it
);
print_r($try_it);
var_dump($try_it);


yields:



Array
(
[self] => Array
(
[self] => Array
*RECURSION*
)

)
array(1) {
["self"]=>
&array(1) {
["self"]=>
&array(1) {
["self"]=>
*RECURSION*
}
}
}


$GLOBALS does, for whatever reason, reference itself, so it'll do about the same thing but with more noise for the non-reflecting portions.

Self-referential data structures used to leak memory but I think that's fixed as well (?)

StupidRalph
07-10-2007, 10:18 AM
Self-referential data structures used to leak memory but I think that's fixed as well (?)
Thanks Ralph (cool name), I really respect your opinions. Since its my post I'll see if I can Google and find out. :)

ralph l mayo
07-10-2007, 12:49 PM
Thanks Ralph (cool name), I really respect your opinions. Since its my post I'll see if I can Google and find out. :)

Thanks, that's nice of you to say.

I googled the leak after I posted but neglect to post back. It's still a consideration in some forms, as this bug is still open http://bugs.php.net/bug.php?id=33595 but it's hard to get bit by it in practice.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum