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 4 of 4
  1. #1
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts

    Array losing its values

    I have an array at the top of my document that when i first use it works properly. However, towards the end of my script when I want to use the same array its empty. print_r does not print anything to the screen. var_dump prints NULL.

    I'm modifying some open source software that has atleast 4 include files between the two times I want to reference my array. I think somewhere along in one of the include files is messing up my array. I'm not too familiar but shouldn't I be able to serialize() my array after I use it the first time. And then unserialize it when I want to use it the second time? This didn't work for me. I even created another array to make sure it wasn't just my array giving me trouble.
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #2
    Regular Coder
    Join Date
    Feb 2007
    Location
    near Washington, DC
    Posts
    135
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Variable Scope?

    This sounds like a problem with the scope of the array. The includes shouldn't have anything to do with it. (I'd need to see your source code to be sure.)

    However, if you define the array in the main routine, then try to access it in a function, you'll get no value. The array isn't gone; it's just out of scope. Here's a simple example:
    PHP Code:
    <?php
    $a 
    'This';   // global $a defined in main program
    that();

    function 
    that() {
        echo 
    $a;   // this refers to the function's local variable $a, not the global $a
    }
    ?>
    This program would produce no output, even though the global $a has been assigned a value. This is because functions keep their own variables, separate from those in the main program. In other words, the global $a is out of scope. (Why? There are many advantages to this kind of partitioning -- not the least of which is assigning each function of a large program to a different programmer or team... the individual programmers then don't have to avoid accidentally duplicating global variables.)

    There are two ways you can solve this. First is the use of the global keyword in the function. If the funcion in the above code is modified like this:
    Code:
    . . .
    function that() {
        global $a;  // now all references to $a mean the global $a
        echo $a;
    }
    . . .
    Now the program will produce the output you might have expected in the first example -- the word "This" will be echoed to the screen.

    With this solution, anything that changes the value of $a in the function will also change the value of $a in the main function, since the function is now working with the global variable.

    The other solution involves passing the value of the global variable to the function as an argument. In our example, you'd change the orginal program like this:
    Code:
    <?php
    $a = 'This';   // global $a defined in main program
    that($a);  // pass the value of $a to the function
    
    function that($a) { // receives a single argument.
        echo $a;   // this refers to the function's local variable $a, not the global $a
    }
    ?>
    Now, the program will again output "This", but there's a subtle difference. If you change the value of $a in the function now, the value of the global $a will not be affected.

    Also note that the variable name in the function need not be the same as the global variable being passed to it. You could just as well say function that($b)... and echo $b. When the function is called, the value of global $a will be passed into the function's local $b.

    Hope this was helpful. If it's below you, I didn't mean to offend. Your question wasn't very clear about the nature of the problem, so this answer is just a (rather long-winded) shot-in-the-dark.
    — Wilford Nusser
    Validate Your Code: (X)HTML CSS
    An HTML Email is NOT a Web Page: HTML Email Guide (1.2Mb pdf) Webmail CSS Support
    REGEX: Brought to you by Psychotic Crack-Smoking Monkeys

  • #3
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Something is overwriting it or unsetting it. Rename/copy the array, or find out what is altering it. Are you sure it's the included files? Try commenting out the includes one at a time and see if your array is still empty at the end.
    PHP Code:
    $array = array(1,2,3);
    $array2 $array
    If you give it a name that isn't used then it won't be altered by other code(You should only do this if the original array needs to be preserved, otherwise just rename the original).

  • #4
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    Ok thanks it was simply a scope problem. As I didn't notice that everything was wrapped in a function call (about 400 lines of code Sounds like someone needs a class). I was defining the array in the global scope and trying to reference it again from within the function which of course is out of its scope. Must've been real tired last night not to see that it was in a function.

    Quote Originally Posted by Inigoesdr View Post
    Something is overwriting it or unsetting it. Rename/copy the array, or find out what is altering it. Are you sure it's the included files? Try commenting out the includes one at a time and see if your array is still empty at the end.
    PHP Code:
    $array = array(1,2,3);
    $array2 $array
    If you give it a name that isn't used then it won't be altered by other code(You should only do this if the original array needs to be preserved, otherwise just rename the original).
    Yes I've tried creating another array to make sure it wasn't simply being overwritten.

    PHP Code:
    $this_is_a_test = array('yeah','uh huh','okay','whats up','shut up'); 
    But it still was still null when I tried to print it out.

    And I tried making it global but I was declaring it global outside of the function. What a goof.
    PHP Code:
    global $this_is_a_test;
    $this_is_a_test = array('yeah','uh huh','okay','whats up','shut up');

    function 
    i_did_not_notice_this() {
    ........
    ........
    ........
    ........
    print_r($this_is_a_test);  //would not print anything.
    var_dump($this_is_a_test);  //would print NULL


    Of course, moving the global keyword should fix this problem. Correct?

    PHP Code:

    $this_is_a_test 
    = array('yeah','uh huh','okay','whats up','shut up');

    function 
    i_did_not_notice_this() {
    ........
    ........
    ........
    ........
    global 
    $this_is_a_test;
    print_r($this_is_a_test);  //would not print out the array indexes and values
    var_dump($this_is_a_test);  //would print out datatype, indexes and values.


    Last edited by StupidRalph; 03-07-2007 at 04:36 PM.
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.


  •  

    Posting Permissions

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