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>
XAMPP already includes PHP, but unless you end the script name with
.php
it is unlikely to be processed by the PHP engine.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:
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.
Stop the apache service, then add one change in c:\xampp\apache\conf\httpd.conf in the section by adding...
Restart apache!
This looks like a big fat 'feature' in the current xampp distribution for win 32-bit.
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 lineswhich 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).
You should add mime type at http conf for instance in apache at httpd.conf entry
I assume you are trying to use php inside .html file? Try adding .htaccess file or changing apache config with the following line: