Home  >  Q&A  >  body text

How to embed php in HTML attribute?

For example, I have a page where php and html are mixed together, and a variable is stored in the php part:

$class = "active";

In HTML I want to use this variable in the attribute of an element:

<p class="">helloworld</p>

For example, here I want to make the value of class of this p be active, which is just stored in the $class variable in php String.
what should I do then? Does JavaScript need to be used?

曾经蜡笔没有小新曾经蜡笔没有小新2678 days ago603

reply all(1)I'll reply

  • 阿神

    阿神2017-05-24 11:36:16

    This is implemented in the PHP stage. It is impossible for the JavaScript code to read the value of the PHP variable.

    <?php
    ……
    $class = 'active';
    ……
    ?>
    <html>
    <head>……</head>
    <body>
    ……
    <p class="<?=$class?>">helloworld</p>
    ……
    </body>
    </html>

    reply
    0
  • Cancelreply