Re: PHP 4.0 Bug #6810 Updated: unset acts strange with a static variable
| From: | Daniel Beckham | Date: | Wed, 20 Sep 2000 14:19:16 +0000 |
| Subject: | Re: PHP 4.0 Bug #6810 Updated: unset acts strange with a static variable | ||
| References: | 1 | Groups: | php.dev |
| Request: | Send a blank email to php-dev+get-33588@lists.php.net to get a copy of this message | ||
I was sort of thinking that was the case. I've expanded the documentation
for unset to include examples of the behavior of unset when used with static
and global variables. Feel free to comment on my text or examples.
Thanks,
Daniel Beckham
----- Original Message -----
From: "Bug Database" <php-dev@lists.php.net>
To: <danbeck@dealnews.com>
Sent: Wednesday, September 20, 2000 5:02 PM
Subject: PHP 4.0 Bug #6810 Updated: unset acts strange with a static
variable
> ID: 6810
> Updated by: stas
> Reported By: danbeck@dealnews.com
> Status: Closed
> Bug Type: *Function Specific
> Assigned To:
> Comments:
>
> Yes, this is how it is meant to work. unset is a bit weird, you probably
shouldn't use it on "special variables" (statics, globals) unless you
understand what you are doing. Use something like $a = "" or $a = false.
>
> Previous Comments:
> --------------------------------------------------------------------------
-
>
> [2000-09-20 04:20:37] danbeck@dealnews.com
> The following code acts weird in respect to unsetting the variable. I'm
trying to clarify unset in the PHP docs and I've come across this
inconsistancy. Something similar to this was mentioned in bug report 2015,
but I think this might be different.
>
> function foo() {
> static $a;
> $a++;
> echo "$an";
>
> // let's unset it once it hits 3
> if ($a == 3) {
> echo "unsetting #$an";
> unset($a);
> }
> echo "value of a: $an";
> }
>
> for ($i = 0; $i < 10; $i++) {
> foo();
> }
>
>
> This generates the following output:
> 1
> value of a: 1
> 2
> value of a: 2
> 3
> unsetting #3
> value of a:
> 4
> value of a: 4
> 5
> value of a: 5
> 6
> value of a: 6
> 7
> value of a: 7
> 8
> value of a: 8
> 9
> value of a: 9
> 10
> value of a: 10
>
>
> When the static variable is unset, it's only unset inside the function.
Instead of $a starting over at 1, it retains it's original value through
each function call.
>
> Not sure if this is intended, but it's very strange behavior.
>
> --------------------------------------------------------------------------
-
>
>
> Full Bug description available at: http://bugs.php.net/?id=6810
>
>