PHP中如何确定增量和减量

发布于 2025-01-04 00:54:22 字数 167 浏览 2 评论 0原文

请向我提供该脚本的正确解决方案并进行解释:

 $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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

鲜肉鲜肉永远不皱 2025-01-11 00:54:22

从阅读上面和下面的脚本到断言,

$var++, $var--  //Use value then apply incremnet, decrement
++$var, --$var  // Increment, decrement then use vakue

您可以重写表达式以便于理解。

$a = 5;
$c = $a--;   // $c = 5, $a = 4
$c += $a--;  //$c = 9, $a = 3
$c += --$a   // $c = 11, $a = 2 ($a drops to two before use)
$c -= --$a   //$c = 10  $a = 1 ($a drops to one before use);

From reading the script above and the following to assertions

$var++, $var--  //Use value then apply incremnet, decrement
++$var, --$var  // Increment, decrement then use vakue

you can rewrite the expression for ease of understanding.

$a = 5;
$c = $a--;   // $c = 5, $a = 4
$c += $a--;  //$c = 9, $a = 3
$c += --$a   // $c = 11, $a = 2 ($a drops to two before use)
$c -= --$a   //$c = 10  $a = 1 ($a drops to one before use);
錯遇了你 2025-01-11 00:54:22

++-- 产生相同的最终结果 - 递增或递减变量 - 无论是在变量名称之前还是之后应用,当它用作变量名称的一部分时,就会出现差异一个更大的声明。

考虑一下:

$a = 5;
$a--;
echo $a; // 4

$a = 5;
--$a;
echo $a; // 4

所以你会看到,它们产生相同的最终结果 - $a 减一。我确信这就是您所期待的。

然而:

$a = 5;
echo $a--; // 5
echo $a; // 4

$a = 5;
echo --$a; // 4
echo $a; // 4

在这个例子中,$a在操作之后仍然是递减的,但是递减发生和值使用的顺序是不同的。对于$a--,该值在递减之前使用,而对于--$a,该值在递减之后使用。

所以对于你的示例代码:

$a = 5;    //  Operations in order of occurence:
$c = $a--  //  $c = 5;          $a = 5 - 1  == 4;
   + $a--  //  $c = 5 + 4 == 9; $a = 4 - 1  == 3;
   + --$a  //  $a = 3 - 1 == 2; $c = 9 + 2  == 11;
   - --$a; //  $a = 2 - 1 == 1; $c = 11 - 1 == 10; 
echo $c;   //  10

++ 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:

$a = 5;
$a--;
echo $a; // 4

$a = 5;
--$a;
echo $a; // 4

So you see, they produce the same end result - $a get decremented by one. I'm sure this is what you were expecting.

However:

$a = 5;
echo $a--; // 5
echo $a; // 4

$a = 5;
echo --$a; // 4
echo $a; // 4

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 = 5;    //  Operations in order of occurence:
$c = $a--  //  $c = 5;          $a = 5 - 1  == 4;
   + $a--  //  $c = 5 + 4 == 9; $a = 4 - 1  == 3;
   + --$a  //  $a = 3 - 1 == 2; $c = 9 + 2  == 11;
   - --$a; //  $a = 2 - 1 == 1; $c = 11 - 1 == 10; 
echo $c;   //  10
伪心 2025-01-11 00:54:22
$a = 5 ; // $a = 5
$c = $a-- // $c = 5 $a = 4
+
$a-- // $c = 9 $a = 3
+
--$a // $c = 11 $a = 2
-
--$a // $c = 10 $a = 1
;
echo $c ; // $c = 10
$a = 5 ; // $a = 5
$c = $a-- // $c = 5 $a = 4
+
$a-- // $c = 9 $a = 3
+
--$a // $c = 11 $a = 2
-
--$a // $c = 10 $a = 1
;
echo $c ; // $c = 10
べ繥欢鉨o。 2025-01-11 00:54:22

表达式 $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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文