Re: Expected behaviour or bug?
| From: | Simon J Welsh | Date: | Mon, 17 Sep 2012 08:55:55 +0000 |
| Subject: | Re: Expected behaviour or bug? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-319109@lists.php.net to get a copy of this message | ||
On 17/09/2012, at 8:50 PM, Camilo Sperberg <unreal4u@gmail.com> 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/