14

I'm new to PHP. I installed XAMPP and have Apache running. I created helloworld.php in XAMPP's htdocs and got PHP to display in my browser. My question is, why does my PHP script in my HTML file not display in my browser? Ive never installed PHP on its own. Should I also install it? Would it conflict with XAMPP. My code is below. Any assistance will be appreciated. Thanks in advance:

<html>
    <body>
        <?php
            echo "Hello PHP World";
        ?>
    </body>
</html>
5
  • Did you intend to post more code? Commented Feb 4, 2010 at 9:54
  • 1
    Is that file in "www" directory in Apache server? (I belive its www, may be www-root) And if it is are you opening it like: localhost/myFile.php? Commented Feb 4, 2010 at 9:57
  • 1
    When I was playing with XAMPP on Windows, all web stuff had to go into a directory called htdocs. Commented Feb 4, 2010 at 10:05
  • 1
    Ok, I was just trying to point that the file must be in a special folder that is the root folder for web sites hosted by that server... And that you must open it in that maner, not like executable (doubleclick). Commented Feb 4, 2010 at 10:10
  • Thanks for replying Andy, Tatu, TRiG and Cipi. @Andy, this is all the code I intended to post. It's my first PHP script in HTML and its not working. @Tatu, the output is a blank browser. @Cipi, I put the helloworld.html file in the htdocs file (i.e xampp/htdocs) and tried two ways: by entering localhost/helloworld.html in the browser and hitting enter, and by double clicking the file's icon. In both cases I got a blank browser @TRiG, I first tried to run my PHP script/HTML file from my desktop but that did not work so I put it in the htdocs file (I am assuming this is the correct file) Commented Feb 4, 2010 at 17:04

6 Answers 6

20

I assume you are trying to use php inside .html file? Try adding .htaccess file or changing apache config with the following line:

AddHandler application/x-httpd-php .html
Sign up to request clarification or add additional context in comments.

2 Comments

Yes I am trying to use php inside an .html file. I was following the example given by w3schools (see link below) and just assumed it would simply work anywhere once I have Apache running: w3schools.com/php/php_syntax.asp Regarding the .htaccess file or changing the apache config, I will have to research that.
My mistake, w3schools did mentioned that you must have a .php extension (not .html) otherwise it would not work
16

XAMPP already includes PHP, but unless you end the script name with .php it is unlikely to be processed by the PHP engine.

5 Comments

Yes but dont you need to put the script in XAMPP's www folder? Or am I wrong...?
@Cipi: Of course. But I prefer to give the asker some benefit of the doubt unless they prove otherwise.
Not just unlikely, it's only configured for .php (and maybe .php5, I forget) out the box. Plain .html (or .htm) will just be served straight back by Apache.
Ohh, ok, I thought I can just put the php script in an html file and it would work anywhere on my harddrive. Did not realize I had to put it in a special place and that I must have a .php extension. Ok Thanks very much. I got it to work by changing the extension from .html to .php. However if I just click on the file, windows does not know the program that created it? Does this mean that I also need to install PHP alongside XAMPP?
Now you need to access it via your browser, by going to localhost or whatnot.
14

Stop the apache service, then add one change in c:\xampp\apache\conf\httpd.conf in the section by adding...

AddType application/x-httpd-php .html .htm  

Restart apache!

This looks like a big fat 'feature' in the current xampp distribution for win 32-bit.

1 Comment

How come I dont see the apache folder? Update: Found it. The httpd.conf is in my etc folder. It won't let you edit directly though so you will have to sudo su before you edit. Thanks btw!
7

You should add mime type at http conf for instance in apache at httpd.conf entry

<IfModule mime_module>
    #
    # TypesConfig points to the file containing the list of mappings from
    # filename extension to MIME-type.
    #
    TypesConfig "conf/mime.types"
   .......
    AddType application/x-httpd-php .html .htm
   AddType text/html .shtml

    AddOutputFilter INCLUDES .shtml
</IfModule>

1 Comment

This is the most useful answer, if not a little brief. XAMPP out of the box does not process php code in .html or .htm out of the box. Add that x-httpd-php line in from this answer and boom it works.
4

The php module for apache registers itself as handler for the mime type application/x-httpd-php. And the configuration file apache\conf\extra\httpd-xampp.conf contains the lines

<FilesMatch "\.php$">
    SetHandler application/x-httpd-php
</FilesMatch>

which tells the apache that all files having .php as name extension are to be processes by the handler for application/x-httpd-php.
If you (really) want to have your .html files handled by the php module as well you have to add something similar for .html extensions. (there are other methods to tell the apache which extension maps to which mime type/handler. But FilesMatch/SetHandler is fine.)
If you want to enable this "feature" for only one directory you can use an .htaccess file to change the configuration for that directory (and its subdirectories).

2 Comments

Thanks for your reply VolkerK. The script is working now that I've changed the .html extension to .php. However, if I click on the file, helloworld.php, Windows does not know the program that created it (even though I have Apache running). I am a bit puzzled, should I also install PHP for Windows?
"if I click on the file" - i.e. double-clicking in the windows explorer? Your Apache and the windows explorer are not connected, they "don't know" of each other. Your Apache handles http requests that e.g. your browser sends.
2

Too much overkill. All these suggestions lead me down the wrong path for like 5 hours. JK, but I did read a lot of google search items all giving wrong answers and each suggestion was just adding more wrong answers.

The answer is in fact so simple you would want to bang your head: Simply change the file extension from ".html" to ".php"!!! Remember that you can build a webpage entirely out of PHP and all JavaScript and stuff built off JavaScript like, JQuery, bootstrap, etc will work.

Here is a simple example of proof:

<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>Blank Web Page</title>

    <link rel="stylesheet" type="text/css" href="css/css.css">

</head>

<body>

    <?php

    $son = 5;
    $nos =10;

    echo $son + $nos;

    ?>

    <h4>test to see if this html element can be output too!</h4>

    <script type="text/javascript" src="js/js.js"></script>

</body>

Notice that I am using your standard html, even though it doesn't show my HTML tags(trust me it's there), web page stuff and have php code inserted inside. Of course the result is 15 and the html element h4 renders correctly too. Change the extension back to "html" and you will get only the h4 element and you will find that your php code has been commented out using multi-comment for html.

I forgot to add that this works for Xampp too.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.