3
<?php
$number1 = 1;
$number2 = 2;

echo $number1.' + ' . $number2. ' = '.$number1+$number2;
?>

参见上面的程序。它给出的输出为 3。为什么不给出输出为:1 + 2 = 3

4

3 回答 3

2

+并且.具有相同的优先级

echo $number1.' + ' . $number2. ' = '.($number1+$number2);
于 2013-07-04T07:50:03.783 回答
1

你想要的输出: -

    <?php
   $number1 = 1;
   $number2 = 2;
   echo $number1.'+ ' . $number2.'='.($number1+$number2);
   ?>
于 2013-07-04T07:59:31.257 回答
0

操作按顺序应用。IE:

echo $number1.' + ' . $number2. ' = '.$number1+$number2;

变成:

echo '1 + 2 = '.$number1+$number2;

变成:

echo '1 + 2 = 1'+$number2;

由于这是附加的,PHP 会将字符串转换为 int,它给出1. 所以最终的表达式是:

echo 1 + 2;//Prints 3

您可以使用括号指示要一起执行哪些操作:

echo $number1.' + ' . $number2. ' = '.($number1+$number2);
于 2013-07-04T07:58:59.980 回答