Jump to content

unset exits loop? (PHP Version 5.2.3)


Grayda

Recommended Posts

As a part of my job, I'm required to build database-driven applications for collecting and maintaining hardware. So far things have been going well. Until I met unset() and loops. Take this sample code:

 

<?php
$arr = array(1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5, 6 => 6, 7 => 7, 8 => ;

foreach($arr as $val) {
unset($val);
}

echo var_dump($arr);

?>

 

Looking over it, it seems pretty easy. 8 items in an array. the foreach loop goes through the array, unsetting each item. What should we expect to see? To me, I expect to see a blank array. But instead, $arr remains untouched and var_dumping $arr produces:

 

array( { [1]=> int(1) [2]=> int(2) [3]=> int(3) [4]=> int(4) [5]=> int(5) [6]=> int(6) [7]=> int(7) [8]=> int( } 

 

Am I missing something here? This happens with any sort of loop I try (foreach, for, do etc.) and with any sort of data I throw at it. The loop runs 8 times (stick an $i++; and an echo and watch it go), but refuses to unset the variable. I doubt this is a scope issue as there are no functions / classes / whatever to get in the way.

 

Cheers :)

Link to comment
https://forums.phpfreaks.com/topic/78362-unset-exits-loop-php-version-523/
Share on other sites

<?php
$arr = array(1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5, 6 => 6, 7 => 7, 8 => ;

foreach($arr as $val) {
unset($val);
}

echo var_dump($arr);

foreach($arr as $key=> $val) {
unset($arr[$key]);
}

echo  '<br> see the chnages here <br>';
echo var_dump($arr);

?>

 

i will not explain just see the sample i gave you ...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.