Best way to unload variables
Moderator: General Moderators
-
php_wiz_kid
- Forum Contributor
- Posts: 181
- Joined: Tue Jun 24, 2003 7:33 pm
Best way to unload variables
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.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
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
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.
It would then trigger errors in parts of code like
Code: Select all
if ($var == 1)What I generally like to do is
$var = array();
Now $var has lost its value, but still exists.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
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.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
![]()
Burr
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
Here's some sample code
What about that? This is actually what I'm doing.
Code: Select all
if(isset($_SESSION['somevar'])) {
unset($_SESSION['somevar']);
}- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
well that works to unset your vars, but I was referring more to something like this.
other stuff in other parts of the page to check if the loggedin var is set:
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...
Code: Select all
if(isset($_GETї'logout'])){
unset($_SESSIONї'loggedin']);
}Code: Select all
if(isset($_SESSIONї'loggedin'])){
echo "e;hello "e;.$_SESSIONї'name'];
}else{
echo "e;you need to log in"e;;
}Burr
edit: oops forgot to end my php tag...