...

View Full Version : Array losing its values



StupidRalph
03-07-2007, 08:21 AM
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.

phoenixshade
03-07-2007, 09:39 AM
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
$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:

. . .
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:

<?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.

Inigoesdr
03-07-2007, 04:50 PM
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.


$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).

StupidRalph
03-07-2007, 05:16 PM
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 :eek: 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. :thumbsup:


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.


$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.



$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.


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?




$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.

}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum