Good morning!
I've spent hours on this to no avail. My understanding is that unset() only deletes the copy of the variable local to the current scope. But I am getting a very different result. I have the following structure and output. What am I doing wrong?
<?php
first_function();
second_function() {
for ($count = 0; $count < $max; $count++) {
$my_array = array[]; //initialize my array, local in this scope but can be called global in nested functions
print_r($my_array ); //print 1: array should be always empty but is only in empty in FIRST loop.
third_function(); //fills the array with numbers, see below, nested function declaring a global array
print_r($my_array ); //print 3 of my_array, should be full of numbers from third_function global changes
unset($my_array); //delete my array so I can start with new array in next loop
print_r($my_array ); //print 4 of my_array, should be unknown variable
print('End one loop');
}
}
third_function() {
global $my_array; //declare my global variable
//...fill my now global array with stuff...
print_r($my_array ); //print 2: should be filled with numbers. And it is.
}
?>
My output amazingly looks something like this
Array()
Array(45,48,38...all my numbers...)
Array()
ERROR: Notice -- Undefined variable: my_array
End one loop
Array(45,48,38...all my SAME numbers again as if array was NOT unset...)
......
The first and second print lines make sense. But shouldn't the third line be the array filled with numbers generated in third_function? The fourth line error makes sense as the variable is unset. But WHAT did I unset? The next time around in the loop, the array contains the SAME numbers from the previous loop and my new numbers simply get appended to the end of the ever growing array. Why?
This should not be that difficult but seems to be driving me crazy. Any help would be greatly appreciated.
alexander