Implementing PHP Code in HTML (.html) Files
The inability to utilize PHP within HTML (.html) pages, as exemplified by index.html, is a common issue. The typical attempts to use both < ? contents ? > and < ?php contents ? > have proven unsuccessful. While PHP support is available on your server and functions properly with the .php extension, the question arises as to whether this is a fundamental issue or requires configuration adjustments within php.ini.
Understanding the Issue
PHP cannot be executed directly within HTML files because the server doesn't recognize this as a valid PHP extension. To resolve this, it is necessary to instruct the server to interpret these files as PHP.
Solution:
Create a file named .htaccess in your web root directory and include the following line:
AddType application/x-httpd-php .htm .html
This directive instructs Apache to process files with .htm or .html extensions as PHP files. Upon implementing this change, PHP will now function as expected within your HTML pages.
The above is the detailed content of How Can I Execute PHP Code Within HTML (.html) Files?. For more information, please follow other related articles on the PHP Chinese website!