PHP中如何确定增量和减量
请向我提供该脚本的正确解决方案并进行解释:
$a = 5;
$c = $a-- + $a-- + --$a - --$a;
echo $c;
What will be the value of $c = 10
;为什么?
Please provide me the proper solution of this script with explanation:
$a = 5;
$c = $a-- + $a-- + --$a - --$a;
echo $c;
What will be the value of $c = 10
; Why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从阅读上面和下面的脚本到断言,
您可以重写表达式以便于理解。
From reading the script above and the following to assertions
you can rewrite the expression for ease of understanding.
++
和--
产生相同的最终结果 - 递增或递减变量 - 无论是在变量名称之前还是之后应用,当它用作变量名称的一部分时,就会出现差异一个更大的声明。考虑一下:
所以你会看到,它们产生相同的最终结果 -
$a
减一。我确信这就是您所期待的。然而:
在这个例子中,
$a
在操作之后仍然是递减的,但是递减发生和值使用的顺序是不同的。对于$a--
,该值在递减之前使用,而对于--$a
,该值在递减之后使用。所以对于你的示例代码:
++
and--
produce the same end result - incrementing or decrementing the variable - whether applied before of after the variable name, the difference comes when it is used as part of a larger statement.Consider this:
So you see, they produce the same end result -
$a
get decremented by one. I'm sure this is what you were expecting.However:
In this example,
$a
is still decremented after the operation, but the order in which the decrement happens and the value is used is different. For$a--
the value is used before the decrement, and for--$a
the value is used after.So for your example code:
表达式
$a--
是后递减的,这意味着它首先返回$a
,然后将$a
减 1。表达式--$a
是预减量,它首先将$a
减 1,然后返回$a
。考虑到上述情况,这意味着
$c = 5 + 4 + 2 - 1 = 10
。The expression
$a--
is post-decrement, which means it first returns$a
and then decrements$a
by one. The expression--$a
is pre-decrement which first decrements$a
by one and then returns$a
.Taking the above into account, this means
$c = 5 + 4 + 2 - 1 = 10
.