Select Category
Sitemap Help Contact
print article

Parsing PHP Code within HTML Pages

The easiest way to parse or run PHP code within an HTML page is to simply change the extension of the page from .html or .htm to .php!

Any regular text or HTML tags within the file will be totally ignored by PHP. Only the code contained within the PHP tags (<? ?>) will be processed by PHP.

If you have many HTML pages that contain PHP code and do not want to go through the trouble of renaming each file (although that method is preferred) you may make a change in the .htaccess file to parse all HTML files as PHP:

Open a text editor such as wordpad, notepad, nano, etc. and add the following line:

AddHandler x-mapp-php5 .html .htm

If you want to use PHP 5.4 instead of PHP 5.2 then use the following line instead:

AddHandler x-mapp-php6 .html .htm

This will specify that all files ending in .html or .htm should be parsed as PHP. Save the file as .htaccess.txt if using a text editor on a Windows machine. Upload this file to your webspace, placing it in the folder containing all the website files for the domain in question. Now Rename the file to .htaccess after it has been uploaded so the changes take effect. The changes will take effect immediately. The .htaccess file will affect all the files in the folder where the .htaccess is saved as well as all the sub-folders and sub-files.

For additional information, you may want to reference: