Home >Backend Development >PHP Tutorial >How Can I Run PHP Code Inside HTML Files?

How Can I Run PHP Code Inside HTML Files?

Barbara Streisand
Barbara StreisandOriginal
2024-12-24 10:18:06775browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn