CoachingUgosay

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

Abinesh S

Abinesh S

Senior WebCoder

Video Thumbnail

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>&copy; 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>&copy; <?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.

More articles

PHP Versions: A Complete Guide from PHP 3 to PHP 8.4

Explore all PHP versions from PHP 3 to PHP 8.4. Timeline, features, performance updates, and recommendations for choosing the right PHP version.

Read more

Generate SSH Key in Linux: How to Do It (Step-by-Step Guide)

Learn how to generate SSH key in Linux. Step-by-step tutorial with commands, sample outputs, and tips for secure authentication without passwords.

Read more

Connect with Us

Got questions or need help with your project? Fill out the form, and our team will get back to you soon. Weโ€™re here for inquiries, collaborations, or anything else you need.

Address
12, Sri Vigneshwara Nagar, Amman Kovil
Saravanampatti, coimbatore, TN, India - 641035