我的HTML代码有什么问题?

file omg.html

<!DOCTYPE html>
<html>
    <head><title>:)</title></head>
        <body>
            <?php
                echo 'Hello world';
            ?>
        </body>
</html>

It's supposed to create a webpage which prints Hello world, but it doesn't print anything. What's wrong with it?

PHP is not HTML.

You need to run your PHP program through a PHP interpreter to output HTML.

This is most commonly performed using:

  • A file with a .php file extension
  • A webserver (such as Apache)
  • Having PHP installed

Other options are available:

  • You can run the file through the command line version of PHP (although this gives you the output at the time and won't be on-demand each time a browser visits the page).
  • You can configure the server to use a different file extension
  • You can configure the server to use a method other than the file extension to determine what is a PHP program.

you can't have php code in an html file..

Make it php file,, so that it can be parsed by server running, most probably apache..

When the server sees a .html as file extension, it simply sends the file to client, which will be interpreted by browser..To run php script, you need file to be .php extension, so that server interpretes it..

You file must have .php extension in order to work.


However, there's a trick to tell Apache to parse HTML files as PHP, if you really want to use .html files as PHP ones. Read more here.

That's not just HTML code, there's PHP code there as well. If the file is called omg.html then the PHP interpreter might not be parsing it. The convention is to name PHP files with the .php extension. You could configure your PHP interpreter to also interpret HTML files, but it's non-standard.

Judging from the question in general, I wonder if a PHP interpreter is involved at all. The file alone can't process PHP code, you need an interpreter. What setup are you using?

Must be run through a server with php. And must have a .php extension to work

save it as .php and check if the file is under apache root where php interpreter is installed

the file extension must have .php

file name should result as "omg.php"