php 中这有什么不同?

发布于 2024-09-17 06:43:05 字数 219 浏览 9 评论 0原文

两者之间有什么区别

$totalprice += $product['price'] * $product['count'];

并且

$totalprice = $product['price'] * $product['count'];

两者给出相同的结果。那么 (+=) 有什么用呢?

what is the difference between

$totalprice += $product['price'] * $product['count'];

and

$totalprice = $product['price'] * $product['count'];

both give the same result. so what's the use of (+=) ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

凝望流年 2024-09-24 06:43:05

+= 是将结果添加到目标的简写。第一个相当于:

$totalprice = $totalprice + ($product['price'] * $product['count']);

还有其他复合运算符 -=< /code>、*=/= 等。

+= is a shorthand for adding the result to the target. The first one is equivalent to:

$totalprice = $totalprice + ($product['price'] * $product['count']);

There are also other compound operators -=, *=, /=, etc.

°如果伤别离去 2024-09-24 06:43:05

仅当 $totalprice 从 0 开始或未初始化时,它们才会给出相同的结果

+= 语法是以下内容的简写:

$myvar += a;

相当于

$myvar = $myvar + a;

They only give the same result if $totalprice starts off at 0 or uninitialised

The += syntax is shorthand for the following:

$myvar += a;

is equivalent to

$myvar = $myvar + a;
一人独醉 2024-09-24 06:43:05

+= 采用 $totalprice 并将 $product['price'] * $product['count'] 添加到其中。
=$product['price'] * $product['count'] 的值分配给 $totalprice

如果您得到相同的结果,那是因为 $totalprice 一开始等于 0。

The += takes $totalprice and adds $product['price'] * $product['count'] to it.
The = asigns the value of $product['price'] * $product['count'] to $totalprice.

If you are getting the same result, its because $totalprice started off equal to 0.

北方的巷 2024-09-24 06:43:05

如果 $totalprice 开始时为零,那么它们是相同的。否则,他们就不同了。

正如其他人指出的那样, $i += $j$i = $i + $j 的简写。

If $totalprice is zero to start, then they're the same. Otherwise, they're different.

As others have pointed out, $i += $j is shorthand for $i = $i + $j.

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