0

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?

Rontogiannis Aristofanis
  • 8,883
  • 8
  • 41
  • 58

7 Answers7

7

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.
Luke
  • 11,426
  • 43
  • 60
  • 69
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • @RondogiannisAristophanes — Install PHP. Give the file a .php file extension. (Then visit the HTTP URI of the program). – Quentin Sep 23 '12 at 14:04
  • How can I visit the HTTP URL of the program, if the program for example is situated at '/home/Desktop/var/a.php'? – Rontogiannis Aristofanis Sep 23 '12 at 14:06
  • @RondogiannisAristophanes — Open your browser. Type http:// then the name or ip address of the server, then a `/` then the path from the document root to the PHP program. – Quentin Sep 23 '12 at 14:08
  • I doubt you have set up your hosts file or DNS to use the hostname "apache" and that you have put the file in [$THE_HTTP_ROOT_DIRECTORY](http://httpd.apache.org/docs/2.2/mod/core.html#documentroot)/root/home/Desktop/var/a.php (as that is a very odd path to choose). So probably not. – Quentin Sep 23 '12 at 16:13
3

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.

Community
  • 1
  • 1
Nikola K.
  • 7,093
  • 13
  • 31
  • 39
1

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..

Rajat Singhal
  • 11,234
  • 5
  • 38
  • 56
  • Why not mate? For example, I have all my pages written in `PHP`, all with `HTML` extension, such as `news.html`, `articles.html`, `albums.html`, etc. See this ["Configure Your Server To Parse HTML Files As PHP"](http://encodable.com/parse_html_files_as_php/) – Wh1T3h4Ck5 Sep 23 '12 at 15:05
  • Yeah..that is also possible..anything is possible..We can ask server to parse any extension file as `php` file, but we shouldn't do that..that creates confusions, and is a bad practise, we should stick to default extensions from std php. ie `.php` or `.php5` etc.. – Rajat Singhal Sep 23 '12 at 15:27
  • of course, it's really bad practice, but still it's possible ;) – Wh1T3h4Ck5 Sep 23 '12 at 18:35
1

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?

David
  • 208,112
  • 36
  • 198
  • 279
0

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

geekman
  • 2,224
  • 13
  • 17
0

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

cc4re
  • 4,821
  • 3
  • 20
  • 27
0

the file extension must have .php

file name should result as "omg.php"

Randomfan
  • 17
  • 3