[英]Why $x=5; $x+++$x++; equals with 11 in PHP?
According to the opcodes it should be 12. Am I getting it wrong? 根据操作码它应该是12.我错了吗?
number of ops: 8
compiled vars: !0 = $x
line #* E I O op fetch ext return operands
-------------------------------------------------------------------------
3 0 E > EXT_STMT
1 ASSIGN !0, 5
5 2 EXT_STMT
3 POST_INC ~2 !0
4 POST_INC ~3 !0
5 ADD ~4 ~2, ~3
6 ECHO ~4
7 7 > RETURN 1
branch: # 0; line: 3- 7; sop: 0; eop: 7; out1: -2
path #1: 0,
Edit 编辑
Also ($x++)+($x++); 另外($ x ++)+($ x ++); returns the same result (11).
返回相同的结果(11)。 Actually this was the main reason for the question and opcode investigation.
实际上,这是问题和操作码调查的主要原因。
It took me a few reads, but $x=5; $x++ + $x++;
它花了我一些读数,但
$x=5; $x++ + $x++;
$x=5; $x++ + $x++;
works like this: 像这样工作:
In the case of a $x++, it first 'gets used', then increased: 在$ x ++的情况下,它首先“被使用”,然后增加:
++
) ($x is now 6, stack=[ 5 ]) ++
)($ x现在是6,stack = [ 5 ]) ++
) (which is isn't used further, but $x is now 7) ++
)(不再使用,但$ x现在为7) Actually, in this specific example, if you would echo $x;
实际上,在这个具体的例子中,如果你要
echo $x;
it would output 7. You never reassign the value back to $x, so $x=7 (you incremented it twice); 它会输出7.你永远不会将值重新分配给$ x,所以$ x = 7(你增加了两次);
$x = 5;
$a = $x++ + $x++;
the expression line will be executed like this: 1st occurrence of $x++
in the statement will increment $x
value by 1 so it will become 6 and 表达式行将像这样执行:语句中第一次出现
$x++
会将$x
值增加1,因此它将变为6并且
in 2nd occurrence, $x
will be having value 6; 在第二次出现时,
$x
将具有值6;
So $a = 5 + 6; 所以$ a = 5 + 6;
So final result $a
will be 11. 所以最终结果
$a
将是11。
++ has higher precedence than + operator ++具有比+运算符更高的优先级
(x++) will return the value of x first then increment it by 1 (x ++)将首先返回x的值,然后将其递增1
$x = 2
$x++ // return 2, then increment it to 3
x+++x++ is evaluated like the following x +++ x ++的评估方式如下
1. Get x value first which is 5
2. Then it will be incremented to 6
3. But first x value will be 5 because (x++) statement will return 5 first then increment the value
4. Then + operator is encountered
5. Next x will have 6 as value not 7 for the same reason (x++) will return the x value first and then increment it
6. So 5+6 is 11
7..At the end, x value will be 7
Same goes for ($x++)+($x++)
($x++)+($x++)
grouping operator ()
has left to right
associatevity. grouping operator ()
具有left to right
相关性。 First ($x++)
executes first. 首先执行
($x++)
。
$x = 5
($x++) returns 5 and then increment $x by 1. Same as before.
then last ($x++) executes. 然后最后($ x ++)执行。 It returns 6 and then increment $x to 7
它返回6然后将$ x增加到7
so same 5+6 // 11
is returned back 所以返回同样的
5+6 // 11
The post increment operator increment the variable, but returns its old value. 后增量运算符增加变量,但返回其旧值。
So $x++
is equivalent to: 所以
$x++
相当于:
($temp = $x, $x = $x + 1, $temp)
When you do it twice in an expression, it's like: 当你在表达式中执行两次时,它就像:
echo ($temp1 = $x, $x = $x + 1, $temp1) + ($temp2 = $x, $x = $x + 1, $temp2);
The first part sets $temp1 = 5
and increments $x
to 6
. 第一部分设置
$temp1 = 5
并将$x
增加到6
。
The second part sets $temp2 = 6
and increments $x
to 7. 第二部分设置
$temp2 = 6
并将$x
增加到7。
Then it does $temp1 + $temp2
and echoes the result, which is 5 + 6 = 11
. 然后它执行
$temp1 + $temp2
并回显结果,即5 + 6 = 11
。
You are using the post-increment operator ($x++). 您正在使用后增量运算符($ x ++)。 If you would like to use the incremented value for the addition you should use the pre-increment operator (++$x).
如果要使用递增的值进行添加,则应使用预增量运算符(++ $ x)。
Therefore if $x = 5 因此,如果$ x = 5
$x++ + $x++ equals 5+6 = 11
++$x + $x++ equals 6+6 = 12
$x++ + ++$x equals 5+7 = 12
$++x + ++$x equals 6+7 = 13
Yet in all cases, x is equal to 7 afterward. 然而在所有情况下,x等于7。
$x = 5;
echo $x++ + $x++;
prints 11 as first $x++ returns 5 and then after that it increments, the second $x++ returns 6 and the only value in incremented. 打印11作为第一个$ x ++返回5然后在它之后递增,第二个$ x ++返回6并且唯一的值递增。 so actual addition is 5+6 which gives 11.
所以实际加法是5 + 6,得到11。
++$x + $x++ will be 12 ++ $ x + $ x ++将是12
And ++$x + ++$x will be 13 而++ $ x + ++ $ x将是13
when you use $x++ $x get +1 as soon it's value is use, but the value that will gonna be use is the one it has before the increment, so when yo do: 当你使用$ x ++ $ x时,只要它的值被使用就得+1,但是将要使用的值是它在增量之前的值,所以当你做的时候:
$x=5; $ X = 5; $x+++$x++;
$ X +++ $ X ++;
$x+++$x++ is 11, but $x will be 7 $ x +++ $ x ++是11,但$ x将是7
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.