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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
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

Add a Patch

Pull Requests

Add a Pull Request

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-2019 The PHP Group
All rights reserved.
Last updated: Mon Jun 17 01:01:28 2019 UTC