php 中这有什么不同?
两者之间有什么区别
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
+=
是将结果添加到目标的简写。第一个相当于:$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.仅当 $totalprice 从 0 开始或未初始化时,它们才会给出相同的结果
+= 语法是以下内容的简写:
相当于
They only give the same result if $totalprice starts off at 0 or uninitialised
The += syntax is shorthand for the following:
is equivalent to
+=
采用$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.如果
$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
.