PHP "Static" 关键字误导其不保留新值

PHP "Static" keyword is misleading its not retaining new value

好的,我的静态关键字有问题

根据下面的w3wchools.com

Normally, when a function is completed/executed, all of its variables are deleted. However, sometimes we want a local variable NOT to be deleted. We need it for a further job.

To do this, use the static keyword when you first declare the variable:

//w3school code below
function myTest() {
    static $x = 0;
    echo $x;
    $x++;
myTest();
myTest();
myTest();
}

现在这基本上是用 ++ 增量运算符计算 0,1,2。很好,它做了静态定义描述的事情

现在如果我用下面的代码以另一种方式基本上做同样的事情... $x 不会递增它只是保持在 0。

//my code version below
function myTest() {
    static $x = 0;
    echo $x;
    $x + 1;
myTest();
myTest();
myTest();
}

这些在理论上基本上是在做同样的事情

w3schools 版本使用 ++ 运算符将 $x 加 1。 increment 表示加

我的版本用 + 运算符加 1...and add 意味着加也

所以可以肯定地说静态只适用于增量运算符吗 而不是基本的数学运算符?

这与static关键字无关。代码 $x+1 根本不会改变 $x 的值,因此它永远不会递增。 $x+=1$x = $x + 1 会有你想要的效果。