Page 1 of 1
How do I unset variable by reference?
Posted: Thu Mar 01, 2007 3:35 pm
by nadavvin
Code: Select all
If a variable that is PASSED BY REFERENCE is unset() inside of a function, only the local variable is destroyed. The variable in the calling environment will retain the same value as before unset() was called.
http://il.php.net/unset
How do I destroyed not only the local variable?
Posted: Thu Mar 01, 2007 4:00 pm
by Ambush Commander
Posted: Thu Mar 01, 2007 4:04 pm
by nadavvin
But the problem that It still exist by over references which point to it.
Posted: Thu Mar 01, 2007 4:06 pm
by Ambush Commander
Are you sure you're actually using references?
Code: Select all
<?php
$var1 = 'asdf';
$var2 =& $var1;
$var2 = null;
echo $var1; // (no output)
?>
$var1 will still "exist" in that it is registered to the symbol table as null, but for all intents and purposes it will be "unset".
Posted: Thu Mar 01, 2007 5:20 pm
by Chris Corbyn
Indeed. unset() might not actuall remove the variable value in the true sense (i.e. it may not clear the memory), it just frees up the namespace. Setting to null is the way to go, and interestingly enough, a (unset) cast just converts to null.
Posted: Thu Mar 01, 2007 6:28 pm
by nickvd
d11wtq wrote:Indeed. unset() might not actuall remove the variable value in the true sense (i.e. it may not clear the memory), it just frees up the namespace. Setting to null is the way to go, and interestingly enough, a (unset) cast just converts to null.
Wow... I was not aware you could cast 'unset'. The things you learn on these forums
Nice use of 42 btw

Posted: Thu Mar 01, 2007 6:30 pm
by Ambush Commander
I was vaguely aware that unset() was not a real function, but that's pretty cool. Can't see any practical use for it though

Posted: Thu Mar 01, 2007 6:40 pm
by nickvd
Ambush Commander wrote:I was vaguely aware that unset() was not a real function, but that's pretty cool. Can't see any practical use for it though

If you are dealing with massive arrays or massive files, and you use unset($var) after you're done with them, would actually free the memory? I read that it would just clean up the variable namespace, but leave any allocated memory alone until the memory is required (similar to a quick versus a full format). But if casting the variable (unset)$var sets it to null (which would free the memory used), it could be easier to read.
Code: Select all
$var = array('massive size');
unset($var); //$var is undefined, but the data remains in ram until overwritten
$var = null; //will clear the memory used, but may not be clear as to why it was set specifically to null
(unset)$var; //seems to be a little clearer as to it's purpose
It's the reason why I use(d) unset(), to make it clear what it's doing. Setting a variable to null can be clear, but it can also have more than a single purpose.
I'm typing quick and I want to go home, hope you can understand my ramblings

Re: How do I unset variable by reference?
Posted: Thu Mar 01, 2007 9:12 pm
by Christopher
nadavvin wrote:How do I destroyed not only the local variable?
In a function you would need to unfortunately do:
Code: Select all
function myfunc() {
unset($GLOBALS['myglobal']);
}
$myglobal = "I'm set!";
echo "myglobal=$myglobal<br/>";
myfunc();
echo "myglobal=$myglobal<br/>";
But please don't. It's bad enough accessing globals, worse setting them, but this!