Re: Possible foreach bug; seeking advice to isolate the problem

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Tommy Pham wrote:
>> -----Original Message-----
>> From: Gary

>> I can tell you how to solve it:
>>     $a = array('a', 'b','c');
>>     foreach($a as &$row){
>>         //you don't have to do anything here
>>     }
>>     unset($row); // <----<<< THIS IS KEY!
>
> Shouldn't that be $row = null since unset will remove the last value,
> not just removing the variable also, from the array whereas the $row =
> null will tell the reference pointer that it doesn't point to a value.

,----[ http://php.net/manual/en/control-structures.foreach.php ]
| ,----[ code ]
| | <?php
| | $arr = array(1, 2, 3, 4);
| | foreach ($arr as &$value) {
| |     $value = $value * 2;
| | }
| | // $arr is now array(2, 4, 6, 8)
| | unset($value); // break the reference with the last element
| | ?>
| `----
| ...
|                                Warning
|  
|  Reference of a $value and the last array element remain even after the
|  foreach loop. It is recommended to destroy it by unset().
`----

If it's wrong, please blame the people who wrote the manual :)


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux