Execute your PHP code from html file:

When web page is loaded, the server will check the extension of the file to know how to handle the page. If it is a .htm or .html file, it sends directly to the browser because it does not have anything to process on server. If it is a .php or .asp etc file, it needs to execute on server.

If you want to add some PHP code in your existing html file, we can do that by two ways.

  1. We can change the file extension as .php, so that the file will execute on the server.
  2. By using your htaccess file you can make your html file as php file as shown below.

Add below line for .html file

AddType application/x-httpd-php .html

for .htm file

AddType application/x-httpd-php .htm

If you want to include the php code only in one page just add the below code to your htaccess file.

<Files yourpage.html>
AddType application/x-httpd-php .html
</Files>

This code will make the PHP code executable on the html file and not in all other files.

Take care while adding the lines to your existing htaccess file.

1. Don’t overwrite your htaccess file, just add extra lines for that as shown above.

2. If your html file have any code which starts with <? will execute as php code. So, echo those lines as below.

<?php echo '<?xml version="1.0" encoding="IUTF-8"?>'; ?>

This coding is only preferable for small websites, which has little php code.