Welcome Guest, Not a member yet? Register   Sign In
Noobish question: Why unset a variable?
#1

[eluser]codex[/eluser]
Everywhere in the CI corelibs I see variables being unset.

Code:
$this->routes = ( ! isset($route) OR ! is_array($route)) ? array() : $route;
        unset($route);

I may be answering my own question, but maybe someone could shed some extra light on the matter (couldn't find an explanation on the interwebs).

a) are vars being unset to free memory?
b) are vars being unset to prevent them from accidentally being used somewhere else?
#2

[eluser]wiredesignz[/eluser]
Yep you answered your own question codex. Wink

unset does both things you describe.
#3

[eluser]codex[/eluser]
[quote author="wiredesignz" date="1221452205"]Yep you answered your own question codex. Wink

unset does both things you describe.[/quote]


Yup, that's kinda what I thought. Thanks for the confirmation! Tongue

(I have never used unset() before)
#4

[eluser]Sumon[/eluser]
I think, unset a variable is a good practice. But if i forget or intentionally don't use unset then this variable is automatically destroyed(freed from memory, or other word unset) after the whole page executed. Right?
#5

[eluser]Mirage[/eluser]
That is correct. PHP automatically cleans up when the execution ends.
#6

[eluser]Michael Wales[/eluser]
Yes - variables will be automatically cleared from memory after script execution. It's much better to just let this happen - could you imagine how hideous your code would be if you unset() every variable at the end of the function - bleh...

Unsetting can be quite powerful though, I find myself usually using them when trying to remove unwanted values from an array (great when comparing two arrays).
#7

[eluser]obiron2[/eluser]
A lot of the time, ! isset is being used to ensure that the module has been reached from the right point and in the right manner.

e.g. is you are trying to visit a secure part of the site and just type in the URL, you might want your code to say

if (! isset($_POST['userID']))

to check that the user ID has been passed as part of the form. If it has not then you know the URL has not been reached from the proper channels.

The other big use for it that I make is to validate checkboxes. If a checkbox is not ticked in a form then the name does not get passed in the form submission as part of $_POST.

if (isset($_POST['permit_emails'])) will return false if the permit emails box was not ticked.


obiron
#8

[eluser]obiron2[/eluser]
sorry, just re-read your question. It wasn't about isset at all

DOH!
#9

[eluser]drewbee[/eluser]
Just to throw this in here since we are talking about freeing memory up,

The same can be said about queries. I generally tend to use mysql_free_result() after running a query if there are still many queries to be made on the same page, or if the query has a large result set and it will no longer be used.

Same as with variables though, PHP automatically cleans all of this up at the end of script execution.


If you have a long running script though, it is almost always a must.
#10

[eluser]taewoo[/eluser]
This is related but off topic too..

FreakAuth Light user authentication library has this problem of user ID/name not being cleared somehow. You have to physically unset the session variables yourself.




Theme © iAndrew 2016 - Forum software by © MyBB