为什么html br break行标记在这段代码中不起作用?

时间:2015-05-25 09:46:46

标签: php html operator-precedence

有人可以告诉我为什么我的php行不能正常工作(回声)吗?

我知道我可以用不同的方式编写代码来使换行工作,但我想知道背后的原因吗?

<?php

    $var1 = 3;

    echo "Addition = "       . $var1 += 3 . "<br>";
    echo "Subtraction = "    . $var1 -= 3 . "<br>";
    echo "Multiplication = " . $var1 *= 3 . "<br>";
    echo "Division = "       . $var1 /= 3 . "<br>";

?>

6 个答案:

答案 0 :(得分:7)

好像我必须在这里清理一些东西。

让我们看看operator precedence,其中说:

  1. .的优先级高于+=-=*=/=
  2. .是左关联的
  3. =+=-=*=/=是正确的关联
  4. 我们还会看一下手册底部的注释:

      

    注意:   虽然 =的优先级低于大多数其他运算符,但PHP仍然允许使用类似于以下的表达式:if(!$ a = foo()),在这种情况下,foo()的返回值为投入$ a。

  5. 意味着即使强硬=的优先级低于.,也会先评估它。如果您执行以下操作,也可以看到此内容:

    $xy = "HERE";
    echo "I am " . $xy = "NOT HERE";
    

    现在您认为.的优先级高于=,并且会先评估,但从手册中的说明开始,分配是第一个,您最终得到的结果是:< / p>

    echo "I am " . ($xy = "NOT HERE");
    

    输出:

    I am NOT HERE
    

    因此,如果我们将所有这些信息放在一起,我们可以说,首先评估分配,但它是正确的关联。意思是:

    $var1 = 3;
    
    echo "Addition = "    . ($var1 += 3 . "<br>");
    echo "Subtraction = " . ($var1 -= 3 . "<br>");
    echo "Addition = "    . ($var1 *= 3 . "<br>");
    echo "Addition = "    . ($var1 /= 3 . "<br>");
    

    所以这段代码最终会出现在:

    echo "Addition = "    . ($var1 += "3<br>");
    echo "Subtraction = " . ($var1 -= "3<br>");
    echo "Addition = "    . ($var1 *= "3<br>");
    echo "Addition = "    . ($var1 /= "3<br>");
    

    然后通过算术运算符得到convert to an integer我们最终得到这个:

    echo "Addition = "    . ($var1 += 3);
    echo "Subtraction = " . ($var1 -= 3);
    echo "Addition = "    . ($var1 *= 3);
    echo "Addition = "    . ($var1 /= 3);
    

    分配完成后,会对连接进行评估,如下所示:

    echo "Addition = "    . 6;
    echo "Subtraction = " . 3;
    echo "Addition = "    . 9;
    echo "Addition = "    . 3;
    

    这样你就得到了这个输出:

    Addition = 6Subtraction = 3Addition = 9Addition = 3
    


    现在如何解决这个问题?只需将作业包装在括号中,以便<br>标记不会进入作业。 E.g。

    echo "Addition = "       . ($var1 += 3) . "<br>";
    echo "Subtraction = "    . ($var1 -= 3) . "<br>";
    echo "Multiplication = " . ($var1 *= 3) . "<br>";
    echo "Division = "       . ($var1 /= 3) . "<br>";
                             //^          ^ So the br tag doesn't get in the assignment of the variable.
    

答案 1 :(得分:4)

由于类型转换问题,这种情况正在发生。在执行操作时,3 . "<br>"将转换为数字。包裹内部(),以便首先执行操作,然后执行连接。

echo "Addition = " . ($var1 += 3) . "<br>";
echo "Subtraction = " . ($var1 -= 3) ."<br>";
echo "Addition = " . ($var1 *= 3) . "<br>";
echo "Addition = " . ($var1 /= 3) ."<br>";

答案 2 :(得分:3)

您可以使用逗号

echo "Addition = " . $var1 += 3 , "<br>";
echo "Subtraction = " . $var1 -= 3 ,"<br>";
echo "Addition = " . $var1 *= 3 , "<br>";
echo "Addition = " . $var1 /= 3 ,"<br>";

或用括号括起来:

echo "Addition = " . ($var1 += 3) . "<br>";
echo "Subtraction = " . ($var1 -= 3) ."<br>";
echo "Addition = " . ($var1 *= 3) . "<br>";
echo "Addition = " . ($var1 /= 3) ."<br>";

否则,3号码会与<br>连接。

答案 3 :(得分:3)

您的PHP意味着:

echo "Addition = " . $var1 += (3 . "<br>");
echo "Subtraction = " . $var1 -= (3 ."<br>");
echo "Addition = " . $var1 *= (3 . "<br>");
echo "Addition = " . $var1 /= (3 ."<br>");

number + 3 . '<br>'number + (int)(3 . '<br>')number + 3。由于重新输入数字(转换为数字),现在不存在<br>

在方程式周围使用括号。

echo "Addition = " . ($var1 += 3) . "<br>";
echo "Subtraction = " . ($var1 -= 3) ."<br>";
echo "Addition = " . ($var1 *= 3) . "<br>";
echo "Addition = " . ($var1 /= 3) ."<br>";

答案 4 :(得分:1)

试试这个..

“”。用于php变量来连接数字

<?php


$var1 = 3;

echo "Addition = ". ($var1 += 3) ."</br>";
echo "Subtraction = ". ($var1 -= 3) ."</br>";
echo "Addition = ". ($var1 *= 3) ."</br>";
echo "Addition = ". ($var1 /= 3) ."</br>";

?>

答案 5 :(得分:1)

试试这种方式。

 <?php
$var1 = 3;

echo "Addition =" . ($var1 += 3 ).'<br>';
echo "Subtraction =" . ($var1 -= 3).'<br>';
echo "Addition =" . ($var1 *= 3 ).'<br>';
echo "Addition =" . ($var1 /= 3 ).'<br>';

?>