php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #6810 unset acts strange with a static variable
Submitted: 2000-09-20 04:20 UTC Modified: 2000-09-20 18:02 UTC
From: danbeck at dealnews dot com Assigned:
Status: Closed Package: *Function Specific
PHP Version: 4.0.2 OS: Linux (Redhat 6.1)
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: danbeck at dealnews dot com
New email:
PHP Version: OS:

 

 [2000-09-20 04:20 UTC] danbeck at dealnews dot 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 "$a\n";

    // let's unset it once it hits 3
    if ($a == 3) {
        echo "unsetting #$a\n";
        unset($a);
    }
    echo "value of a: $a\n";
}

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.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2000-09-20 18:02 UTC] stas@php.net
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Nov 15 01:01:36 2024 UTC