tags:

views:

158

answers:

3

Hi, 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>
+1  A: 

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

Ignacio Vazquez-Abrams
Yes but dont you need to put the script in XAMPP's www folder? Or am I wrong...?
Cipi
@Cipi: Of course. But I prefer to give the asker *some* benefit of the doubt unless they prove otherwise.
Ignacio Vazquez-Abrams
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.
Paolo
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?
01010011
*Now* you need to access it via your browser, by going to http://localhost/ or whatnot.
Ignacio Vazquez-Abrams
Good thanks it works
01010011
+1  A: 

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

VolkerK
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?
01010011
"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.
VolkerK
Ok, thanks very much VolkerK, I understand now.
01010011
A: 

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
Artjom Kurapov
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: http://www.w3schools.com/php/php_syntax.aspRegarding the .htaccess file or changing the apache config, I will have to research that.
01010011
My mistake, w3schools did mentioned that you must have a .php extension (not .html) otherwise it would not work
01010011