You are here: Re: Don't unset that variable you need globally « PHP Programming Language « IT news, forums, messages
Re: Don't unset that variable you need globally

Posted by Hilarion on 10/15/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]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация