I see the usage of the following code snippet:
$cookieexpiry=(time()+21600);
setcookie("rwphp","$cookieid",$cookieexpiry,"/",$_SERVER["HTTP_HOST"],0);
unset($cookieexpiry);
Q1> My question is whether or not this is a good practice to unset $cookieexpiry.
Q2> In general cases, when should I unset variables?
Thank you
You do not need to unset it at all. The garbage collector will take care of it. Only moment I would think you need to unset is if you need the variable to be inexistent later on in your code.
Maybe I am overlooking something.
See here for the doc about unset()
One reason to unset would be to free up some memory after you are done with some HUGE object. Thanks ovais.tariq
The only thing the unset is going to do in this case is free up the variable $cookieexpiry. It is not going to have any affect on your cookie itself.
I think you are a little confused about that because of the name of your variable, but all $cookieexpiry is is the number representing the time and nothing more.
I have seen unset used very rarely, you would unset if you needed that variable name again for something, but I believe most people would use a different variable name if they needed it again.
unsetting the cookieexpiry variable like you have shown is not going to make much difference here because it doesnt hold substantial data,.
unset is useful when you have previously stored some large data in a variable, like for example a complex object and that variable is not being used further
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With