What do you expect to happen? I'd expect it to output "something
";
You never actually call foo().
What do you expect to happen? I'd expect it to output "something
";
You never actually call foo().
Hi Packet, I thought it was supposed to cancel out 'something' buts its not, something still appears on the page.
Weedpacket wrote:You never actually call foo().
Maybe you missed it
<?php
function foo(&$bar)
{
unset($bar);
$bar = "blah";
}
$bar = 'something';
echo "$bar\n"; // something
foo();
echo "$bar\n"; // blah
?>
Note, however, that in this application the unset() is totally unnecessary, since the following would have the exact same functionality:
<?php
function foo(&$bar)
{
$bar = "blah";
}
?>
Right Nog, you're great, I was just trying to learn how to use it, I'll try that. I don't care about foo that was just an example taken from the manual. I tried replacing it with another named function with the same result.
Nog are you there? On the first one I have two "somethings" and no "blah" which looks like the unset is keeping the blah from appearing? Which is what I want to happen so that is great. But I am getting an error message on the first one which is no big deal but I suppose if I am using correctly I probably need an example of one without errors. It says there is a missing argument for this, "function foo(&$bar)" even if I change foo to something else in all areas. Anyone know how to get rid of it? Or why it is happening so I can get rid of it? Thanks.
My reply was incorrect. As per http://www.php.net/unset (which it looks like you got your example from):
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.
So, when you do the unset($bar), you destroy the local reference, but not the original variable that it is referencing. Then when you next do $bar="blah", you are creating a new $bar which is local to the function. Therefore, it's value is unknown to the code outside of the function.
So the key here is that the unset() is only unset()-ing the reference, not the variable that it is referencing. Does that help any, or just muddy the waters more?
Well let me try, basically I think it means, if I put it inside a function basically it won't work on any variable at all? So I need to keep it outside a function to get it to affect an actual variable? If thats the case, gosh why are most of their examples inside functions, it really messes people like me up. The scary part is I think I got it working now thanks to you. I did this <?php
$t = 'bar';
unset($t);
echo $t;
?>
And it worked!!! Is there anything on this rule I am missing? Thanks a bunch.
I think most of those examples are intended to show how unset()-ing a variable within a function definition has no effect on variables outside of that function. (Their secondary intention was to confuse you. )
Lets show all the things it can't do lol, thanks Nog. Oh dear more testing, it can work on things inside the funtcion then? I'll check it out.
The variable inside the function is a different variable from the one outside; the fact that they have the same name is a coincidence (well, not a coincidence, because it's deliberate, but incidental, anyway). Think of every variable inside foo() as having a ghostly "... belonging to function foo()" following it around.
When
function foo(&$bar)
{
unset($bar);
$bar = "blah";
}
$bar = 'something';
foo($bar);
is run it creates a variable $bar and points it to the value "something". When foo() is called a new variable $bar ("... belonging to function foo()") is created that is set to point to the same value as the original $bar.
Then unset() destroys that $bar ("... belonging to function foo()") variable. This leaves the original untouched.
Then finally an entirely new variable named $bar ("... belonging to function foo()") is created to point to the value "blah". This new variable has nothing to do with the previous one of the same name. That one has been destroyed.
Giving all these distinct variables different names to emphasise that they are different variables the code becomes
function foo(&$oink)
{
unset($oink);
$glee = "blah";
}
$bar = 'something';
foo($bar);
I really thought they were all related to each other. Why not use different names then instead of the same names?
Jazzy Girl wrote:I really thought they were all related to each other. Why not use different names then instead of the same names?
To quote myself from my last reply: "Their secondary intention was to confuse you. "
You should see their certification exam.
"Answer: D, not A. Did you notice the extra semicolon?"