unset() static variable doesn't work?

限于喜欢 提交于 2019-12-02 02:42:36

If a static variable is unset, it destroys the variable only in the function in which it is unset. The following calls to the function (getValues()) will make use of the value before it was unset.

This is mentioned the documentation of the unset function as well. http://php.net/manual/en/function.unset.php

From the Doc

If a static variable is unset() inside of a function, unset() destroys the variable only in the context of the rest of a function. Following calls will restore the previous value of a variable.

function foo()
{
    static $bar;
    $bar++;
    echo "Before unset: $bar, ";
    unset($bar);
    $bar = 23;
    echo "after unset: $bar\n";
}

foo();
foo();
foo();

The above example will output:

Before unset: 1, after unset: 23
Before unset: 2, after unset: 23
Before unset: 3, after unset: 23
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!