Who can explain in detail the PHP $a = 10; $b = $a--+--$a; Implementation process?
When I was reading Gaulo's book, "PHP," I Found
$a = 10;$b = $a++ + ++$a; //书上写的执行过程是$a先自增1后再自增1,再赋给$becho $a; //$a = 12;echo $b; //$b = 22;$b = $a-- - --$a; //$a先自增1后再自增1,再赋给$becho $a; //$a = 10echo $b; //$b = 2
If it's logical, then add
$b = $a-- + --$a;echo $a; //$a = 8;echo $b; //$b = 18;
Can someone please tell me the detailed execution of this code?
Reply content:
Who can explain in detail the PHP $a = 10; $b = $a--+--$a; Implementation process?
When I was reading Gaulo's book, "PHP," I Found
$a = 10;$b = $a++ + ++$a; //书上写的执行过程是$a先自增1后再自增1,再赋给$becho $a; //$a = 12;echo $b; //$b = 22;$b = $a-- - --$a; //$a先自增1后再自增1,再赋给$becho $a; //$a = 10echo $b; //$b = 2
If it's logical, then add
$b = $a-- + --$a;echo $a; //$a = 8;echo $b; //$b = 18;
Can someone please tell me the detailed execution of this code?
Sweat, the three pieces of code are the same, it is necessary to put so much? Whether it is $a++
or not ++$a
, you just have to understand that it's better to read the code from left to right.
$a++
The value of the first is $a
taken out, and then self-added 1, so the $b = $a++;
result of the output should be$a=11;$b=10;
++$a
The meaning is to do the $a
self-add 1, and then the $a
value is taken out, so $b = ++$a;
the result of the output should be$a = 11;$b=11;
If you can understand the above two paragraphs, it is not a problem to understand your calculation according to this idea. I'll tell you the last one:
$b = $a-- + --$a;
This should be broken down for easy understanding.
$c = $a--;$d = --$a;$b = $c + $d;
As explained above, the $a--
value is returned here $a
, and then the $a
self minus 1, at this point $a=9;$c=10;
. --$a
here is the $a
self minus 1 and then $a
the return value, so at this point $a=8;$d=8;
. The final result is naturally $a=8;$b=18;
.