• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Noobish question: Why unset a variable?

Everywhere in the CI corelibs I see variables being unset.

$this->routes = ( ! isset($route) OR ! is_array($route)) ? array() : $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?

Yep you answered your own question codex. Wink

unset does both things you describe.

[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)

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?

That is correct. PHP automatically cleans up when the execution ends.

[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).

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.


sorry, just re-read your question. It wasn't about isset at all


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.

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.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.