I'm a newbie here trying to figure out why my simple php code is not working inside html. Files with .php extension however works fine.The page is just blank. I modified the following line
"AddType application/x-httpd-php .php" in http.conf to "AddType application/x-httpd-php .php .html"
I tried adding these lines to .htaccess file.
AddHandler x-httpd-php .html .htm
AddHandler php-script .php .html .htm
AddHandler php5-script .php .html .htm and
AddType application/x-httpd-php .htm
AddType application/x-httpd-php .html as said in most of the forums. Nothing seems to work. Please someone help me out.I'm using wamp server 2.2 and php 5.

You can't run PHP in an .html file, it has to be a .php file

commented: That's incorrect. +0

If you haven't come across this resource already, I found it useful:
http://www.velvetblues.com/web-development-blog/how-to-parse-html-files-as-php/

It talks about the type of handler you need to use based on whether you have one or multiple versions of PHP installed. If using multiple, you need to explicitly specify application/x-httpd-php5 .html, but there are a couple of other workarounds to try too.

I ended up using the following in a .htaccess file:

AddHandler application/x-httpd-php .html

And the following in a HTML file:

<h1>Hello <?php echo 'World'; ?></h1>

Like simplypixie said, You won't be able to run PHP code within HTML files (e.g index.html) so the files have to be with the extension of .php (e.g index.php). The reason being is that you're telling the browser to interpert PHP code within HTML which doesn't make sense to the borswer. It will render it as a simple text. However, if you do it the other way around, it will render html code within your php file without any issue.

I hope this helps you a little. If you post your code here so we can have a look and tell you if it is broken or not.

@blocblue: why is that incorrect?

commented: Much better explanation than mine :-) +7

@rotten69 - because you can tell the web server to parse HTML files for PHP before serving the content to the browser.

Browsers do not understand PHP, even in PHP files. It's a server side scripting langauage. So even in PHP files, the file is parsed and only resulting output sent to the browser.

@blocblue.. Thanks for the reply. My answer is precise to the question. PHP code will NEVER run in HTML file unless technology has evolved ?? Just to be sure, I tested putting PHP code in HTML file that is living on web server. It didn't work.

Correct, it doesn't work out-of-the-box on most hosts, but it does work if you add the correct handler to the htaccess file (as specified by bloclue).

@rotten69 - sorry, but you're wrong. If you read the full thread, you'll see that you can in fact configure a web server to parse a HTML file for PHP.

A web server won't do this natively so just putting PHP code in HTML file will not work.

Member Avatar for diafol

I think the correct responses by pritaeas and blocblue are quite clear - if you're still there priya and you're trying to wade through the argument. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.