Unset(array element) at the end


          $langs = array("JavaScript",
          "HTML/CSS", "PHP",
          "Python", "Ruby");
          foreach ($langs as $lang) {
              echo "<li>$lang</li>";

What purpose does unset($lang); serve here?


It means you destroy the variable named $langs

Add same foreach loop after unset

also you can remove a single element of an array.like



Apologies, but this answer actually only raised more questions for me.

Firstly, this is my initial contact with PHP, so just to be clear by 'destroy' a variable do you mean to remove references to a particular memory location or actually destroy destroy that piece of particular data? Because wouldn't that increase the machine's overhead?

Secondly, I guess I didn't make question clear enough, but what I wanted to know why is unset($lang); outside of foreach? Variable $lang is a local variable that exists inside the foreach brackets, how can unset() access a local variable? I am viewing this from a Python&Java background, am I wrong?

Finally, say if the unset() works as intended, the other question from me is why is it necessary to 'destroy' the variable here. Is it always a good practice to destroy the iterated variable used in foreach?


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.