Using unset with foreach

Okay, so I’m not entirely a n00b, but I was on php.net on the foreach manual page: http://php.net/manual/en/control-structures.foreach.php

and it mentioned using unset to destroy remaining references:

Reference of a $value and the last array element remain even after the foreach loop. It is recommended to destroy it by unset().

I’ve haven’t seen any of the developers where I work do this. Is it necessary? What is the purpose of doing this?

Here’s my code:
(im basically populating a table with data held in an array from a class.

<?php
							$hold_insurers = $ca_state->get_state_insurer_array();
							$temp_counter = 0;
							
							foreach($hold_insurers as $parent_key => $parent_value){
							  $temp_counter++;
							  if($temp_counter%2 == 0){
							  	echo "<tr class=\\"stripe\\">\
";
							  }
							  else echo "<tr>\
";
							
							  echo "\	<td class=\\"insurer_logo\\">\
";
							  echo $parent_key; // insurer name (hopefully)
							  echo "\	</td>\
";
							  echo "\	<td class=\\"insurer_jd_power\\">\
";
								  if($parent_value[5] == "N/A") {
									echo "N/A";							  	
								  }
								  else {
								  	echo "<span class=\\"stars_".$parent_value[5]."\\"></span>"; // insurer J.D. Power ranking
								  }
							  echo "\	</td>\
";
							  echo "\	<td class=\\"insurer_cust_sat\\">\
";
							  echo $parent_value[6]; // insurer customer satisfaction
							  echo "\	</td>\
";
							  echo "\	<td class=\\"insurer_free_quote\\">\
";
							  echo "<a href=\\"#\\" class=\\"insurer_free_quote_btn\\"></a>"; // free quote button
							  echo "\	</td>\
";
							  echo "</tr>\
";
							}

						?>

Any advice/tips are welcome and much appreciated. Feel free to critique anything else I have written there.

[B]Here is why you want to unset:

[LIST=1]
[]Stop Newbie Reference Errors
[
]Stop Newbie Declaration Errors
[*]Free Memory
[/LIST][/B]

foreach($values as $i => &$value) {
    $value = rand();
    unset($value);
}

If you don’t unset, every element of $values will be the first value of $values[0]; it’s a very newbie mistake.

Another big reason you want to unset everything in a loop is to stop newbie errors. For example:

foreach($values as $i => $value) {
    if($value == 5) {
        $next = true;
    }
    unset($value);
    unset($next);
}

If you don’t unset, $next will be true for every iteration after.

Ok, so I understand what you said and it makes sense, but it brought up another question:

In your example(s), without using unset is there ever a risk of $value or $next being referenced or overriding something in a context outside of the foreach?

Sorry if all of this is super rudimentary, I’m just getting back into learning more than basic concepts and my grasp of scope still needs strengthening.

Variables are removed from memory when the script finishes. So IMHO you don’t need to worry about it much unless they’re holding a lot (eg. a BIG array) and/or the script still has a ways to go (i.e. want to make more room in memory for the new stuff) before it ends.

In fact, I suspect it’s more of a drain to unset a simple boolean than it is to simply leave it.

Add to that, if you mistakenly unset a variable that you do need later, you’ve introduced an error.

I think that’s why you don’t see it being used more often.

That said, there are times when you might want to clear out a bunch. I guess the usual example is to loop through POST/GET/REQUEST vars and unset them all. Although this was more of a custom back in the evil GLOBALS on days.

The answer yes. And thats very important to note. Loops do not hold their own scope.

Soooo what would you expect to happen here?:

$value = 'something is here!';

$values = array('foo', 'bar', 'baz')
foreach($values as $i => $value) {
    if($value == 5) {
        $next = true;
    }
    unset($next);
}

echo $value;

Well the echo will output: “baz”. You lost the context of the first definition. This is another big mistake in templating with PHP.

The same goes with…

$value = 'something is here!';

$values = array('foo', 'bar', 'baz')
foreach($values as $i => &$value) {
    if($value == 5) {
        $next = true;
    }
    unset($next);
    unset($value)
}

echo $value;

The output of $value will be NULL or “”.