0

I'm trying to learn HTML and PHP. I grabbed the XAMPP installer for Windows and started the Apache and MySQL server, created an HTML file as shown beneath - but the PHP code doesn't run. I do have a PHP directory on C:\XAMPP, but it doesn't look like PHP is properly installed / working.

<!DOCTYPE html>
<html>
<body>

<h1>Test</h1>

<?php
echo "Hello World!";
?>

</body>
</html>

What am I doing wrong here?

Erik
  • 2,500
  • 6
  • 28
  • 49
  • 3
    check file extension is `.php`, put your file in `c:/xampp/htdocs/` directory, access in browser via `http://localhost/file_name.php` – viral Sep 23 '15 at 07:39
  • There it works. So C:\xampp\htdocs is the root of the apache web server - where I would place my index.html files, etc? I opened it off my desktop you see, and hence PHP didn't run as expected – Erik Sep 23 '15 at 07:41
  • Yes, exactly. but, `.html` files will not be parsed, until you configure your Apache server to do so (treat `.html` as `.php`). But, don't do this until you completely understand the flow. Also you need to put your files in `C:\xampp\htdocs` so the apache can parse it and serve you via port `80`, which is same as requesting it with browser. (sending HTTP GET request) – viral Sep 23 '15 at 07:45
  • I see. I removed all the files from the htdocs directory now and created an index.php file which opens correctly when I enter localhost or 127.0.0.1 in a webbrowser. What makes apache know that index.php is the root file? Does it have a list or something or does it automatically open the file named "index" regardless of extension? – Erik Sep 23 '15 at 07:49
  • Most(probably all) `*ampp` packages come with Apache configured like this. It searches for `index.html` and `index.php` first, if found then serves it, if not then, it will show directory listing of current directory, You can also configure this behavior by creating `.htaccess` file in your `htdocs` folder. Search what `.htaccess` does, and you will be good to go – viral Sep 23 '15 at 07:58

1 Answers1

0

As @Viral said it is because php parser does not parse .html files you can change it in vhost, httpd.conf or .htaccess add this directive if you want html files to be parsed

AddType application/x-httpd-php .php .html

You can also find file named httpd-xampp.conf and change line <FilesMatch "\.php$"> to <FilesMatch "\.(php|html)$">

You should remmeber that sometimes it is better to leave it this way and put your php code in php files. While you get familiar with PHP check what MVC is.

Robert
  • 19,800
  • 5
  • 55
  • 85