Convert HTML to PHP: Step-by-Step Guide for Dynamic Websites

Senior WebCoder

Many beginners start by building static HTML sites, but sooner or later they need to upgrade. However, if you want to convert HTML to PHP, this guide will show you step by step how to transform a static site into a dynamic PHP website thatโs easier to maintain and scale.
By making the switch, you can:
- Reuse layouts (headers, footers, menus) using PHP includes.
- Manage content dynamically with variables.
- Store and retrieve blog posts or product data from a database.
- Therefore, improve scalability and simplify website updates.
This tutorial provides a real-world portfolio project to demonstrate the process. Moreover, you will see how PHP helps reduce repetitive work and adds flexibility.
๐ฏ Project Overview
Before (Static HTML Website):
portfolio-site/
โโโ index.html
โโโ about.html
โโโ contact.html
โโโ style.css
After (Dynamic PHP Website):
portfolio-site/
โโโ index.php
โโโ about.php
โโโ contact.php
โโโ header.php
โโโ footer.php
โโโ db.php (optional for blog)
โโโ style.css
Step 1 โ Original Static HTML (index.html)
<!DOCTYPE html>
<html>
<head>
<title>My Portfolio</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<header>
<h1>My Portfolio</h1>
<nav>
<a href="index.html">Home</a>
<a href="about.html">About</a>
<a href="contact.html">Contact</a>
</nav>
</header>
<main>
<h2>Welcome</h2>
<p>This is my static portfolio site.</p>
</main>
<footer>
<p>© 2025 My Portfolio</p>
</footer>
</body>
</html>
โ Problem: Each file (index.html
, about.html
, contact.html
) repeats the same header and footer.
Step 2 โ Convert .html
to .php
Rename files:
index.html
โindex.php
about.html
โabout.php
contact.html
โcontact.php
Now these files can run PHP code.
Step 3 โ Create Reusable Components
header.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title><?php echo $pageTitle; ?> | My Portfolio</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<header>
<h1>My Portfolio</h1>
<nav>
<a href="index.php">Home</a>
<a href="about.php">About</a>
<a href="contact.php">Contact</a>
</nav>
</header>
footer.php
<footer>
<p>© <?php echo date("Y"); ?> My Portfolio</p>
</footer>
</body>
</html>
Step 4 โ Convert HTML to PHP with Includes
index.php
<?php $pageTitle = "Home"; ?>
<?php include 'header.php'; ?>
<main>
<h2>Welcome</h2>
<p>This is my first dynamic PHP website created after I convert HTML to PHP.</p>
</main>
<?php include 'footer.php'; ?>
about.php
<?php $pageTitle = "About"; ?>
<?php include 'header.php'; ?>
<main>
<h2>About Me</h2>
<p>I converted my static HTML website into a dynamic PHP website to make it easier to update.</p>
</main>
<?php include 'footer.php'; ?>
contact.php
<?php $pageTitle = "Contact"; ?>
<?php include 'header.php'; ?>
<main>
<h2>Contact Me</h2>
<form method="post" action="contact.php">
<label>Name: <input type="text" name="name"></label><br>
<label>Message: <textarea name="message"></textarea></label><br>
<button type="submit">Send</button>
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
echo "<p>Thanks, " . htmlspecialchars($_POST['name']) . "! Your message has been received.</p>";
}
?>
</main>
<?php include 'footer.php'; ?>
โ Now your site has one header and one footer file. Update once, and the changes apply to all pages.
Step 5 โ Add Database Support (Optional)
To make the site more powerful, letโs add a blog section.
db.php
<?php
$conn = new mysqli("localhost", "root", "", "portfolio");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
blog.php
<?php $pageTitle = "Blog"; ?>
<?php include 'header.php'; ?>
<?php include 'db.php'; ?>
<main>
<h2>My Blog</h2>
<?php
$result = $conn->query("SELECT title, content FROM posts");
while($row = $result->fetch_assoc()) {
echo "<article>";
echo "<h3>" . $row['title'] . "</h3>";
echo "<p>" . $row['content'] . "</p>";
echo "</article>";
}
?>
</main>
<?php include 'footer.php'; ?>
โ Final Project Features
- Reusable header & footer with PHP includes.
- Dynamic titles using
$pageTitle
. - Contact form with PHP processing.
- Blog section using MySQL.
- Fully converted static HTML to dynamic PHP website.
โ FAQs
Q1: Why should I convert HTML to PHP? Because PHP makes websites dynamic, reusable, and easier to manage. You update one file instead of many.
Q2: Do I always need MySQL? Not necessarily. You can use PHP includes for small websites. Use MySQL for blogs, shops, or user accounts.
Q3: Is PHP still used in 2025? Yes. Over 75% of websites, including WordPress, still rely on PHP.
Q4: Can I mix HTML and PHP?
Absolutely. PHP works inside .php
files along with normal HTML.
Conclusion
Converting static HTML into PHP allows you to:
- Reuse layouts with includes.
- Add dynamic features with variables.
- In addition, integrate databases for blogs, shops, and dashboards.
- As a result, your static site becomes easier to maintain and extend.
This transformation makes your site easier to maintain and extend.
Try These Examples
- ๐ Set up a LAMP Stack and practice running PHP on your local machine.
- ๐ Create a WordPress site โ powered by PHP and MySQL.
- ๐ Explore PHP Versions to choose the right one for your project.