Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP script not working in HTML file

Tags:

php

xampp

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>
like image 342
Anthony Avatar asked Feb 04 '10 09:02

Anthony


People also ask

Why is my PHP code not working in HTML?

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.

How do I get PHP code to work in HTML file?

As you can see, you can use any HTML you want without doing anything special or extra in your PHP file, as long as it's outside and separate from the PHP tags. In other words, if you want to insert PHP code into an HTML file, just write the PHP anywhere you want (so long as they're inside the PHP tags).

Why PHP script is not running in browser?

PHP is not intended for execution in a browser. It is for web servers to execute, or other preprocessing on the PHP-installed computer. PHP runs in several incarnations when installed on a computer: from the command line.

Can we save PHP in HTML file?

If you really want to serve your PHP code with . html files, it can be done. Your web server is configured to detect the file type by looking at the extension.


2 Answers

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.

like image 100
user2301506 Avatar answered Oct 22 '22 14:10

user2301506


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

like image 42
Ignacio Vazquez-Abrams Avatar answered Oct 22 '22 12:10

Ignacio Vazquez-Abrams