On 17/09/2012, at 8:50 PM, Camilo Sperberg <[email protected]> wrote:
> 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.
That's expected, as per http://nz.php.net/unset:
"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."
---
Simon Welsh
Admin of http://simon.geek.nz/
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php