Best way to unload variables

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
php_wiz_kid
Forum Contributor
Posts: 181
Joined: Tue Jun 24, 2003 7:33 pm

Best way to unload variables

Post by php_wiz_kid »

I was just wanting to know which was is better to unload unwanted variables (such as $_SESSION variables). I'm currently using the unset() function, but I was wondering if setting the variable to null would be better practice. Thanks in advance.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Unset is fine, but it will completely unset the varialble, as in it won't exist anymore.

It would then trigger errors in parts of code like

Code: Select all

if ($var == 1)
You will now have un undefined error.
What I generally like to do is

$var = array();

Now $var has lost its value, but still exists.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

I use unset(), but I always check if my vars exist with isset() and then put my var stuff in those blocks...guess it's a matter of personal preference...gonna try your way phenom

:lol:

Burr
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Burrito wrote:I use unset(), but I always check if my vars exist with isset() and then put my var stuff in those blocks...guess it's a matter of personal preference...gonna try your way phenom

:lol:

Burr
You are correct, you should infact have it properly setup so if the var may not exist, you should be checking to see if it exists, and then check for your expected values.

Sometimes however, a blank value is what you require, sometimes no var..
all depends on the situatin and how you have it setup, I guess..
php_wiz_kid
Forum Contributor
Posts: 181
Joined: Tue Jun 24, 2003 7:33 pm

Post by php_wiz_kid »

Here's some sample code

Code: Select all

if(isset($_SESSION['somevar'])) {
  unset($_SESSION['somevar']);
}
What about that? This is actually what I'm doing.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Depends what you want to do with it? Completely erase the variable? or erase only its value..

This generally is not a problem with smaller applicatins, but is very important how you handle your variable in large scale applications.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

well that works to unset your vars, but I was referring more to something like this.

Code: Select all

if(isset($_GETї'logout'])){
unset($_SESSIONї'loggedin']);
}
other stuff in other parts of the page to check if the loggedin var is set:

Code: Select all

if(isset($_SESSIONї'loggedin'])){
echo &quote;hello &quote;.$_SESSIONї'name'];
}else{
echo &quote;you need to log in&quote;;
}
that way you won't get errors if you try to call the $_SESSION['loggedin'] var if it's not set.

Burr

edit: oops forgot to end my php tag...
Post Reply