|
Posted by Hilarion on 11/21/26 11:20
> Steve wrote:
> > Say you have defined a variable $var in your main script.
> >
> > Now in a function you access it using:
> > global $var;
> >
> > But you want to set it to null inside the function.
> > DO: $var = null;
> > DONT DO: unset($var);
> >
> > Why? Because the 2nd way removes the variable from the name space,
> > and it is no longer globally available. Cost me 10 hours....
Kimmo Laine wrote:
> That's odd. The manual seems to disagree:
>
> "If a globalized variable 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.
>
> <?php
> function destroy_foo()
> {
> global $foo;
> unset($foo);
> }
>
> $foo = 'bar';
> destroy_foo();
> echo $foo; // prints "bar"
> ?>"
> - from http://fi.php.net/manual/en/function.unset.php
That's exactly what Steve said. He said that the variable is removed
from current namespace (function namespace), not that it's totaly
removed.
"global" keyword is like setting local reference to global variable.
When you call "unset", then you unset reference to that variable.
To "unset" global variable from a function you'll have to
"unset( $_GLOBAL['foo'] )".
Hilarion
Navigation:
[Reply to this message]
|