Jump to content

the purpose of the unset function


johnmerlino1

Recommended Posts

In C, when you dynamically allocate memory, you have to free it, otherwise you face memory leaks:

char *str;
str = (char *) malloc(10);
free(str);

But in most other languages you do not need to worry about this. PHP has a function called unset, which unsets the variable's value. But why would you want to do this, if PHP has a garbage collector that will free variables once they leave scope anyway? I see no point of unset in a scripting language with garbage collection.

Link to comment
Share on other sites

It can be useful as a hint to PHP that the variable is no longer necessary and the memory can be collected. If you're doing something in a loop and might end up reading in a lot of data on a particular iteration adding unset can help get PHP to collect that memory sooner.

 

The primary reason I use it however is to break references, for example if you use a foreach loop with the value as a reference:

foreach ($array as &$value){
  //do stuff
}
After the loop $value will still be a reference to the last item of the array so if you happen to later on do something like:

foreach ($array as $value){
}
you'll end up over-writing the last index with each of the indexes before it. Adding unset($value) after the first loop will prevent this by breaking the reference between $value and the last index.
Link to comment
Share on other sites

I second ginerjm. It allows me to clear a lot of session data at once without removing all of the session data. For example, on a language learning site I structure $_SESSION as a multi-dimensional array. I have some things like login data that I don't want cleared but I want to clear data on the deck of flashcards that the user is working on. unset ($_SESSION['card_deck']); clears all the data on the flashcards while leaving everything else alone.

Link to comment
Share on other sites

I second ginerjm. It allows me to clear a lot of session data at once without removing all of the session data. For example, on a language learning site I structure $_SESSION as a multi-dimensional array. I have some things like login data that I don't want cleared but I want to clear data on the deck of flashcards that the user is working on. unset ($_SESSION['card_deck']); clears all the data on the flashcards while leaving everything else alone.

So basically unset is the equivalent of setting an initialized value in Java back to null.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.