增量值时的奇怪行为

Here is my code

$f1  = 1;
$f2  = ++$f1;
$f3  = ++$f2;
$f4  = ++$f3;

echo  $f1 . '<br />'.$f2.'<br />'.$f3.'<br />' .$f4. '<br />';

Output is:

2
3
4
4

I expected $f4 value to be 5, but it is 4. What I miss ?

$f1  = 1;

Assigns 1 to $f1.

$f2  = ++$f1;

Pre-increments $f1. So it is now 2. And this is assigned to $f2. Both $f1 and $f2 at this point are 2.

$f3  = ++$f2;

Pre-increments $f2. So it is now 3. And this is assigned to $f3. Both $f2 and $f3 at this point are 3.

$f4  = ++$f3;

Pre-increments $f4. So it is now 4. And this is assigned to $f4. Both $f3 and $f4 at this point are 4.

The operator ++ actually increments the variable. What you want is $fn + 1;

In the last case $f3 holds 3, is incremented to 4 and 4 is assigned to $f4.

$f1 = 1;
$f2  = ++$f1; //both $f1 and $f2 = 2
$f3  = ++$f2; //both = 3
$f4  = ++$f3; //both = 4

$f1 is 1; you pre-increment it and assign 2 to $f2. Same for 3 and $f3, 4 and $f4. The values match the number the whole way down.

Or, another way: you start with 1. You have three increment operations. Thus the final result must be 4.

It quite logical, Follow along

$f1 = 1; The number 1 is assigned to $f1, nothing special here

$f2 = ++$f1; You pre-increment $f1, so that becomes $f1=2. Then this value is assigned to $f2. So $f=2 also

$f3 = ++$f2; Here you pre-increment $f2, so $f2 becomes '3', and then you assign that value to $f3. So $f3=3

$f4 = ++$f3; Next, you pre-increment $f3. Since it was '3', it now becomes '4'. And then that value is assigned to $f4, which also becomes '4'. Thus this leaves you with

2 3 4 4