The value of unset()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tores
Forum Contributor
Posts: 120
Joined: Fri Jun 18, 2004 3:04 am

The value of unset()

Post 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
LiquidPro
Forum Commoner
Posts: 37
Joined: Wed Aug 04, 2004 6:33 pm
Location: Akron, OH

Post 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.
ldomingues
Forum Commoner
Posts: 41
Joined: Fri Aug 06, 2004 1:15 pm
Location: Portugal

Post 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

?>
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post 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();
Post Reply