Re: Expected behaviour or bug?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 17 sep. 2012, at 10:55, Frank Arensmeier <farensmeier@xxxxxxxxx> wrote:

> 17 sep 2012 kl. 10.50 skrev Camilo Sperberg:
> 
>> Hello list, I have a little question with PHP's internal working. I've managed to reduce the test to the following lines:
>> 
>> $globalVariable = 'i am a global variable';
>> function testFunction() {
>> 	global $globalVariable;
>> 	unset($globalVariable);
>> }
>> 
>> testFunction();
>> 
>> if(isset($globalVariable)) {
>> 	var_dump('global variable IS set');
>> } else {
>> 	var_dump('global variable is NOT set');
>> }
>> 
>> 
>> 
>> When executing the above test, you will get printed that the global variable is set, despite unsetting it in the function. Is it really the intention to unset a global variable inside a function locally or have I just happen to found a little bug?
>> 
>> unreal4u-MBP:~ unreal4u$ php --version
>> PHP 5.3.13 with Suhosin-Patch (cli) (built: Jun 20 2012 17:05:20) 
>> Copyright (c) 1997-2012 The PHP Group
>> Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies
>>   with Xdebug v2.3.0dev, Copyright (c) 2002-2012, by Derick Rethans
>> 
>> If it is expected behavior, is there any documentation on why this is done this way?
>> 
>> Greetings and thanks.
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>> 
> Sometimes, it helps reading the manual...
> 
> http://lmgtfy.com/?q=php+unset+global
> 
> "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."
> [...]
> "To unset() a global variable inside of a function, then use the$GLOBALS array to do so:"
> 
> Took about 1 minute to find out.
> 
> /frank

Doh! Only thing I can say is that I tried searching but without any relevant terms… Thanks and also thanks to Simon!

Greetings.
-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux