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>
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.
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).
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.
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.
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.
XAMPP already includes PHP, but unless you end the script name with .php
it is unlikely to be processed by the PHP engine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With