How do I unset variable by reference?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nadavvin
Forum Commoner
Posts: 68
Joined: Wed Sep 06, 2006 6:05 am

How do I unset variable by reference?

Post 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?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Code: Select all

$var = null;
nadavvin
Forum Commoner
Posts: 68
Joined: Wed Sep 06, 2006 6:05 am

Post by nadavvin »

Ambush Commander wrote:

Code: Select all

$var = null;
But the problem that It still exist by over references which point to it.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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".
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.

Code: Select all

$var = 42;
var_dump((unset)$var);
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post 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.

Code: Select all

$var = 42;
var_dump((unset)$var);
Wow... I was not aware you could cast 'unset'. The things you learn on these forums ;)


Nice use of 42 btw :)
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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 :P
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post 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 :P
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 ;)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How do I unset variable by reference?

Post 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!
(#10850)
Post Reply