Execute PHP From an HTML File

Man using a computer

Troels Graugaard / Getty Images

PHP is a server-side programming language that is used in conjunction with HTML to enhance the features of a website. It can be used to add a log-in screen or a survey, redirect visitors, create a calendar, send and receive cookies, and more. If your website is already published on the web, you'll need to alter it a bit to use the PHP code with the page.

When a webpage is accessed, the server checks the extension to know how to handle the page. Generally speaking, if it sees a .htm or .html file, it sends it right to the browser because it doesn't have anything to process on the server. If it sees a .php extension, it knows that it needs to execute the appropriate code before passing it along to the browser.


You find the perfect script, and you want to run it on your website, but you need to include PHP on your page for it to work. You could just rename your pages to yourpage.php instead of yourpage.html, but you may already have incoming links or search engine ranking, so you don't want to change the file name. What can you do?

If you are creating a new file anyway, you may as well use .php, but the way to execute PHP on a .html page is to modify the .htaccess file. This file may be hidden, so depending upon your FTP program, you may have to modify some settings to see it. Then you just need to add this line for .html:

AddType application/x-httpd-php .html

or for .htm:

AddType application/x-httpd-php .htm

If you only plan on including the PHP on one page, it is better to set it up this way:

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

This code makes the PHP executable only on the yourpage.html file and not on all of your HTML pages.


  • If you have an existing .htaccess file, add the supplied code to it, do not overwrite it or other settings may stop working. Always be cautious when working on your .htaccess file and ask your host if you need help.
  • Anything in your .html files that starts with <? will now be executed as PHP, so if it's in your file for some other reason (as an XML tag, for example), you need to echo these lines to prevent errors. For example, use: <?php echo '<?xml version="1.0" encoding="IUTF-8"?>'; ?>
mla apa chicago
Your Citation
Bradley, Angela. "Execute PHP From an HTML File." ThoughtCo, Aug. 26, 2020, thoughtco.com/execute-php-from-a-html-file-2693780. Bradley, Angela. (2020, August 26). Execute PHP From an HTML File. Retrieved from https://www.thoughtco.com/execute-php-from-a-html-file-2693780 Bradley, Angela. "Execute PHP From an HTML File." ThoughtCo. https://www.thoughtco.com/execute-php-from-a-html-file-2693780 (accessed March 27, 2023).