无法在HTML中显示PHP文件结果[重复]

This question already has an answer here:

Good afternoon,

So I met a problem, I can't display PHP file results in HTML.

As example I have php file named hi.php that contains code like this:

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

And I have index.html that contains body like this:

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

And I get a blank page... No results displayed from hi.php. I'm working on localhost, xampp. May this be the problem, or I have something wrong?

</div>

Two problems:

  • your HTML file should have the .php extension for it to understand PHP code change index.html to index.php

  • you are not using include to include the code onto your HTML

    Do it this way:

    index.php

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

Create a file index.php with that content:

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

That should give you an idea of how things work.


There obviously are endless variants. For example you can also do this:

File index.php:

<?php
$hi="Hello!";

echo <<<EOT
<!DOCTYPE html>
<html>
<body>
{$hi}
</body>
</html>
EOT;