我知道在 php 中可以像这样连接两个字符串
$a .= $b;
等于
$a = $a . $b;
是否可以用整数做某事?但是用数学运算?我有两个变量:
$var1 = 8;
$var2 = 2;
我需要执行 $var1 - $var2
,但我不想创建第三个变量来保存此计算。我会尝试 $var1 = $var1 + $var2
这行得通吗?是否有另一种 .=
类似的方法?
最佳答案
好吧,因为现在大家都很困惑
// _Appending_ $var2 to $var1 and assign it to $var1
$var1 .= $var2;
// _Add_ $var2 to $var1 and assign the result to $var1
$var1 += $var2;
没有什么比每个 binary operator is combineable with the assignment 更值得记住的了。 .在引擎中,它总是扩展为
$var1 = $var1 . $var2; // You can imagine how the others looks like
In addition to the basic assignment operator, there are "combined operators" for all of the binary arithmetic, array union and string operators that allow you to use a value in an expression and then set its value to the result of that expression.
关于php - 在php中连接两个整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12109925/