How To Include PHP Code In .html Pages

Knowing how to include PHP code in HTML pages allows you to add dynamic features, such as contact forms, content includes, or date/time functions, without changing your file structure. This can be useful for developers who want to keep using .html files while still benefiting from PHP’s server-side functionality. At Domains.co.za, our Web Hosting plans support PHP out of the box. With just a few tweaks, you can enable PHP code execution within your .html pages and unlock powerful features for your site — without renaming every file to .php.

How to Include PHP Code in .html Pages

1. Login to your Domains.co.za account.

How to Include PHP Code in .html Pages - Domains.co.za Login Page

2. Select your hosting plan and click Manage next to your domain name.

How to Include PHP Code in .html Pages - Select Web Hosting

3. Click Login next to Control Panel to access the cPanel Dashboard.

How to Include PHP Code in .html Pages - Manage Hosting

4. Open the File Manager under the Files section.

How to Include PHP Code in .html Pages - Open File Manager

5. Navigate to the root directory of your website (public_html).

How to Include PHP Code in .html Pages - Access Root Directory

6. Find the file named .htaccess. If it doesn’t exist, you can create one by right-clicking> Create New File and name it .htaccess.

7. Right-click on the .htaccess file and select Edit.

8. Add the following line at the bottom of the file:

AddType application/x-httpd-php .html .htm

9. Save your changes. This tells the server to treat .html and .htm files as PHP scripts.

10. Now open any .html file and insert your PHP code wherever needed, such as:

<?php echo "Today is " . date("l, F jS Y"); ?>

11. Visit the page in your browser. Your PHP code will now execute inside the .html file.

Additional Information

What Is PHP?

PHP, which stands for PHP: Hypertext Preprocessor, is a widely used open-source, server-side scripting language designed for web development.

When you visit a website, your web browser sends a request to a web server. If that request is for a PHP file, the PHP code inside that file is executed on the server (not in your browser). The PHP interpreter processes the code, generates dynamic content (often HTML), and then sends that generated content back to your browser. Your browser then displays the final HTML, CSS, and JavaScript that make up your site.

At its core, PHP serves as the “brain” of a website. It handles the logic, interacts with data, and generates the HTML that your browser displays. It’s what makes websites interactive and dynamic, going beyond simple static pages.

Why Include PHP Code in .html Pages

No Need to Rename Files: A significant benefit is that you don’t have to rename your existing .html files to .php. This means your current URLs remain unchanged, preventing broken links and maintaining your SEO (Search Engine Optimisation).

Server Compatibility: Most web servers, particularly Apache (which is widely used by hosting providers like Domains.co.za and many others), can be configured to process .html files as PHP. This is typically done via a simple rule in the .htaccess file.

No Server Restart Required: When you modify the .htaccess file to enable PHP processing for .html files, the changes usually take effect immediately. There’s no need to restart the web server, making it a quick and seamless configuration.

Efficient Code Reuse: By enabling PHP in your .html files, you can use PHP’s include or require functions. This lets you reuse common code blocks, such as navigation menus, footers, or headers, across multiple pages. When you need to update these elements, you only need to change them in one central file, and the changes are reflected everywhere they are included.

It is important to note that while this method offers convenience, the most common practice for PHP development is using .php file extensions. However, in specific scenarios like maintaining existing .html URLs or adding dynamic elements to static .html pages, this method can make things much easier.

What Our Customers say...