CodeIgniter Forums

Full Version: Noobish question: Why unset a variable?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

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?

El Forum

Yep you answered your own question codex. Wink

unset does both things you describe.

El Forum

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

El Forum

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?

El Forum

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

El Forum

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

El Forum

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.


El Forum

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


El Forum

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.

El Forum

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.