在 PHP echo 语句中添加多个类

add Multiple classes in a PHP echo statement

您好,我正在尝试添加多个 class,同时回应一些 html,但它似乎不起作用。 PHP 如下所示:

echo
"<div class="."alert alert-success"." role="."alert".">
        <strong>Well done!</strong> You successfully read this important alert message.
</div>";

输出看起来像这样(使用 chrome 上的开发工具捕获):

<div class="alert" alert-success="" role="alert">
        <strong>Well done!</strong> You successfully read this important alert message.
</div>

它已在警报成功时自动填充了 ="",但官方看起来是这样

<div class="alert" alert-success role="alert">
        <strong>Well done!</strong> You successfully read this important alert message.
</div>

出于某种原因,警报成功 class 似乎放在括号之后。我试过在 " 周围移动,但它仍然在同一个地方。我缺少一些简单的东西吗?

试试 -

echo
"<div class='alert alert-success' role='alert'>
        <strong>Well done!</strong> You successfully read this important alert message.
</div>";
echo
  "<div class='alert alert-success' role='alert'>
    <strong>Well done!</strong> You successfully read this important alert message.
 </div>";

实际输出是:

<div class=alert alert-success role=alert>
        <strong>Well done!</strong> You successfully read this important alert message.
</div>

原因很简单:PHP 中的字符串用双引号括起来,并且在您的代码中您永远不会在字符串中放置双引号:您使用它们来关闭字符串。

要在用双引号括起的 PHP 字符串中使用双引号,您需要通过在其前面添加反斜杠来转义该字符:

echo
"<div class=\"alert alert-success\" role=\"alert\">
        <strong>Well done!</strong> You successfully read this important alert message.
</div>";

问题是双码和单码,所以这是行不通的。如果你想使用双码我们反斜杠。

class=\"alert alert-success\" 角色=\"alert\"

这是运行成功谢谢。

"干得好!您已成功阅读此重要提醒消息";