|  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
 [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;
    echo "$a\n";

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

for ($i = 0; $i < 10; $i++) {

This generates the following output:
value of a: 1
value of a: 2
unsetting #3
value of a: 
value of a: 4
value of a: 5
value of a: 6
value of a: 7
value of a: 8
value of a: 9
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.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2000-09-20 18:02 UTC]
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-2020 The PHP Group
All rights reserved.
Last updated: Tue Jun 02 21:01:27 2020 UTC