为什么 html br 换行标记在此代码中不起作用?

Why doesn't the html br break line tag doesn't work in this code?

谁能告诉我为什么我的 php 换行符不起作用(回显)?

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

<?php

    $var1 = 3;

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

?>

可以使用逗号,

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> 连接。

您的 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>";

试试这个..

“。”用于 php 变量连接而不是数字

<?php


$var1 = 3;

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

?>

试试这个方法。

 <?php
$var1 = 3;

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

?>

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

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

看来我必须清理这里的一些东西。

让我们看一下 operator precedence,它说:

  1. . 的优先级高于 +=-=*=/=

  2. . 左结合

  3. =,+=,-=,*=,/=是右结合

  4. 我们也看看手册底部的注释:

    Note: Although = has a lower precedence than most other operators, PHP will still allow expressions similar to the following: if (!$a = foo()), in which case the return value of foo() is put into $a.

意味着即使是 tough = 也比 . 优先级低,它首先被评估。如果你这样做,你也可以看到这个:

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

现在你会认为 .= 有更高的优先级并且会先被评估,但是从手册中的注释来看,赋值是第一位的,你最终得到这个:

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> 标签就不会进入作业。例如

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.