johnmerlino1 Posted July 29, 2014 Share Posted July 29, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/290162-the-purpose-of-the-unset-function/ Share on other sites More sharing options...
kicken Posted July 29, 2014 Share Posted July 29, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/290162-the-purpose-of-the-unset-function/#findComment-1486368 Share on other sites More sharing options...
davidannis Posted July 29, 2014 Share Posted July 29, 2014 I use it to quickly clear part of an array. For example, if I have $myarray['category']['subcategory']['itemtype']['item'] I can unset($myarray['Honda']['Accord']); Quote Link to comment https://forums.phpfreaks.com/topic/290162-the-purpose-of-the-unset-function/#findComment-1486380 Share on other sites More sharing options...
Zane Posted July 29, 2014 Share Posted July 29, 2014 (edited) Same as kicken and daviddennis, I usually only find myself using it on array elements. Edited July 29, 2014 by Zane Quote Link to comment https://forums.phpfreaks.com/topic/290162-the-purpose-of-the-unset-function/#findComment-1486384 Share on other sites More sharing options...
ginerjm Posted July 29, 2014 Share Posted July 29, 2014 I find it most useful when I want to remove a session variable. Obviously php won't clean those up and at times some of them need to be deleted. 1 Quote Link to comment https://forums.phpfreaks.com/topic/290162-the-purpose-of-the-unset-function/#findComment-1486388 Share on other sites More sharing options...
davidannis Posted July 29, 2014 Share Posted July 29, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/290162-the-purpose-of-the-unset-function/#findComment-1486411 Share on other sites More sharing options...
johnmerlino1 Posted July 29, 2014 Author Share Posted July 29, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/290162-the-purpose-of-the-unset-function/#findComment-1486438 Share on other sites More sharing options...
trq Posted July 30, 2014 Share Posted July 30, 2014 So basically unset is the equivalent of setting an initialized value in Java back to null. I don't think so. Unsetting a variable is like it was never initialized at all. Quote Link to comment https://forums.phpfreaks.com/topic/290162-the-purpose-of-the-unset-function/#findComment-1486444 Share on other sites More sharing options...
Jacques1 Posted July 30, 2014 Share Posted July 30, 2014 (edited) C, Java. What language comes next? I suggest Brainfuck. Edited July 30, 2014 by Jacques1 1 Quote Link to comment https://forums.phpfreaks.com/topic/290162-the-purpose-of-the-unset-function/#findComment-1486449 Share on other sites More sharing options...
ginerjm Posted July 30, 2014 Share Posted July 30, 2014 (edited) Why are we even debating this? The OP has to get used to a new environment. Let's let him get used to it instead of finding fault with every nuance that is 'nu' to him. Edited July 30, 2014 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/290162-the-purpose-of-the-unset-function/#findComment-1486453 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.