无法在 HTML 中显示 PHP 文件结果


Can't show PHP file result in HTML

下午好,

所以我遇到了一个问题,我无法在 HTML 中显示 PHP 文件结果。

例如,我有一个名为 hi.php 的 php 文件,其中包含如下代码:

<?PHP
$hi="Hello!";
echo "$hi";
?>

我有index.html包含这样的身体:

<!DOCTYPE html>
<html>
<body>
<?php include "taskai.php" ?>
</body>
</html>

我得到一个空白页...hi.php 未显示结果。我正在本地主机xampp上工作。这可能是问题所在,还是我出了什么问题?

两个问题:

  • 您的 HTML 文件应该具有 .php 扩展名,以便理解 PHP 代码将index.html更改为index.php

  • 您没有使用include将代码包含在 HTML 中

    这样做:

    索引.php

    <!DOCTYPE html>
    <html>
        <body>
         <?php 
         include "taskai.php" 
         ?>
        </body>
    </html>
    

创建一个包含该内容的文件index.php

<!DOCTYPE html>
<html>
<body>
<?php
$hi="Hello!";
echo "$hi";
?>
</body>
</html>

这应该让你了解事情是如何运作的。


显然有无穷无尽的变体。例如,您也可以这样做:

文件index.php

<?php
$hi="Hello!";
echo <<<EOT
<!DOCTYPE html>
<html>
<body>
{$hi}
</body>
</html>
EOT;