Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Can';当使用赋值运算符时,t回显字符串?_Php - Fatal编程技术网

Php Can';当使用赋值运算符时,t回显字符串?

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

当我使用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 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