Page 1 of 1

The value of unset()

Posted: Fri Aug 06, 2004 6:30 am
by tores
Hi all

Just want to start a little discussion about unsetting variables in php. I'd like to know your view on when unset($var) should be used...
Everytime your finished using a variable, everytime your finished using a "large" variable or never...
What's the common practise?

Personally I seldom unset variabels. Maybe I want to use the variable later, and has forgotten that somewhere the variable was unset.
I don't take unset() too seriously in other words.

regards tores

Posted: Fri Aug 06, 2004 8:22 am
by LiquidPro
I'm in the same boat as you on that subject.

The main time that I use unset() is when I'm finished using a class. So if I have my SQL class initialized as $sql, I'll simply unset it at the end of my program.

Posted: Fri Aug 06, 2004 1:20 pm
by ldomingues
I always use unset when i need to "delete" a value in an associative array.

For example:

Code: Select all

<?php
$fields["id"]["description"]="pkey";
$fields["id"]["value"]=10;
$fields["name"]["description"]="name";

//if you want to remove part of the array do:

unset($fields["name"]);

// for example

?>

Posted: Fri Aug 06, 2004 1:43 pm
by hawleyjr
php's Garbage collection will take care of all unused variables. Unless the variable is extremely large, don’t waste your time with unset();