Home >Backend Development >PHP Tutorial >How Can I Run PHP Code Inside HTML Files?
Running PHP in HTML Files: A Solution
You're struggling to execute PHP code within your HTML files, despite having PHP enabled on your server. This is because HTML files are not natively recognized as PHP files by servers.
Solution: Configure Apache Using .htaccess
To resolve this issue, you need to create a .htaccess file in your website's root directory and add the following line:
AddType application/x-httpd-php .htm .html
This configuration instructs the Apache web server to treat files with .htm or .html extensions as PHP files.
Example HTML File with PHP Code:
<!DOCTYPE html> <html> <body> <?php echo "Hello, world!"; ?> </body> </html>
After implementing this change, your HTML files will be processed as PHP, allowing you to utilize PHP code within them.
The above is the detailed content of How Can I Run PHP Code Inside HTML Files?. For more information, please follow other related articles on the PHP Chinese website!