使用 php 中的变量打印文本时,如何在 html 中使用标题标签


How to use heading tags in html when printing a text using a variable in php

我有这个代码。

PHP编码

<body>
    <?php
        $var="PHP TUTORIAL";
        echo $var;
    ?>  
    <p>PHP is a hypertext preprocessor</p>
</body>

我想知道在打印$var时如何在 html 中使用标签。

你能在<?php ... ?>中使用html代码吗

当然

可以!所有 PHP 输出都将由浏览器发送和处理,包括其中的 HTML。

<body>
    <?php echo "<h1>This is a PHP header 1!</h1>"; ?>
    <h2>This is an HTML header 2!</h2>
    <?php echo "<h3>This is a PHP header 3"; ?> that's also half HTML!</h3>
    <?php echo "<h4>This is a variable: ".$var."</h4>"; ?>
</body>

这将在浏览器中输出,如下所示:

<body>
    <h1>This is a PHP header 1!</h1>
    <h2>This is an HTML header 2!</h2>
    <h3>This is a PHP header 3 that's also half HTML!</h3>
    <h4>This is a variable: foobar</h4>
</body>