Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts

    $_GLOBALS array and array_walk

    If I used array_walk_recursive on the $GLOBALS array would I have an infinite loop?


    Edit: ERRORS:
    - I initially said array_walk but meant array_walk_recursive.
    - I also had $_GLOBALS and of course meant $GLOBALS.
    Last edited by StupidRalph; 07-09-2007 at 07:55 AM. Reason: I had the wrong function name and mispelled the $GLOBALS array
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #2
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    No. $_GLOBALS does not exist.

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

  • #3
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts

    Eek!

    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.
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #4
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    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.

  • #5
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    Nope. I've read it correctly. However this book might be somewhat dated. Its O'reilly's Programming PHP. 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).
    Last edited by StupidRalph; 07-10-2007 at 07:29 AM. Reason: Forgot to add parenthesis after var_dump() function
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #6
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    Rasmus' description of var_dump is accurate but print_r has been fixed.

    PHP Code:
    $try_it = array(
        
    'self' => &$try_it
    );
    print_r($try_it);
    var_dump($try_it); 
    yields:

    Code:
    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 (?)
    Last edited by ralph l mayo; 07-10-2007 at 08:45 AM.

  • #7
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    Quote Originally Posted by ralph l mayo View Post
    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.
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #8
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    Quote Originally Posted by StupidRalph View Post
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •