frustratedprogrammer
frustratedprogrammer

Reputation: 25

Unsetting a variable after foreach

Is it a good practice to unset the variable after foreach?

heres a sample code:

foreach ($totalwins as $key => $value) {
//code here
}

unset($key);

Upvotes: 1

Views: 573

Answers (2)

Brian Muenzenmeyer
Brian Muenzenmeyer

Reputation: 1036

Most conventions would determine that future foreach loops define their own first-use of the $key. It comes down comfort with your code. As Devon said in the comments if you are really worried about it you could use distinct variable names, but this is not always possible.

Upvotes: 0

Prime
Prime

Reputation: 2482

Unless the object is taking up a significant amount of memory it makes practically no difference whether or not you unset it unless it's necessary for it to be unset, in which case I'd recommend rethinking that specific part of your process because I can't see a valid reason for it to be necessary let alone mandatory.

Upvotes: 1

Related Questions