Php Can';当使用赋值运算符时,t回显字符串?
当我使用foreach运行变量数组时,我发现在使用加法运算符时,不会显示回显字符串。但是只有echo赋值运算符才能显示回显字符串。无法使用加法运算符回显字符串的原因是什么Php Can';当使用赋值运算符时,t回显字符串?,php,Php,当我使用foreach运行变量数组时,我发现在使用加法运算符时,不会显示回显字符串。但是只有echo赋值运算符才能显示回显字符串。无法使用加法运算符回显字符串的原因是什么 <?php $key = 3; echo $key+1;echo "<br>"; // 4 echo "The answer is ".++$key; // The answer is 4 echo "The answer is ".$key+1; // 1 //last echo is
<?php
$key = 3;
echo $key+1;echo "<br>"; // 4
echo "The answer is ".++$key; // The answer is 4
echo "The answer is ".$key+1; // 1
//last echo is why can't display string and not getting 4
?>
这是因为您正在使用字符串进行数学运算
“答案是,“$key+1
与
"The answer is 3" + 1 which equals 1;
您需要使用()来清除范围
$key = 3;
"The answer is ".($key+1) === "The answer is 4"
也
echo“答案是”。($key+1)
你应该得到5
,而不是4
你是对的@MarkBaker,它是5 by++@MarkBaker,他的问题是为什么?所以答案是,+
操作符在字符串处。比如这里,因为首先创建字符串“答案是1”,然后将其添加到4,非空非数字字符串转换为0。
echo "The answer is ".++$key; // The answer is 4
echo "The answer is ".($key+1); // This would be 5, beause you're incrementing $key by 1 beforehand