Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a good practice to unset cookie expire parameter?

Tags:

php

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

like image 503
q0987 Avatar asked Aug 19 '10 19:08

q0987


3 Answers

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

like image 78
Iznogood Avatar answered Nov 01 '22 17:11

Iznogood


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.

like image 43
Christa Avatar answered Nov 01 '22 17:11

Christa


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

like image 1
ovais.tariq Avatar answered Nov 01 '22 17:11

ovais.tariq