HTML to PHP refers to the process of converting static HTML content into dynamic PHP code. In PHP, you can use HTML to create the structure of a webpage while embedding PHP code to make the page dynamic, handle form submissions, connect to a database, or generate content dynamically.
In essence, you can embed PHP inside HTML tags to create web pages that interact with databases, handle user input, and perform server-side logic. This allows for creating dynamic content such as user profiles, product listings, or blog posts.
Why Convert HTML to PHP?
Dynamic Content: With PHP, you can generate dynamic content based on data from databases, user input, or external APIs. This helps create more interactive and personalized web pages.
Server-Side Logic: PHP can execute logic on the server-side, such as handling form submissions, performing calculations, and generating different content based on user interaction or environment.
Database Interaction: PHP is commonly used with databases like MySQL or SQLite, allowing you to generate HTML content based on data stored in these databases (e.g., displaying a list of products from a database).
Reusable Code: By converting static HTML to PHP, you can reuse common code structures (like headers, footers, and navigation menus) across multiple pages, making it easier to maintain and update.
Steps to Convert HTML to PHP
Embed PHP in HTML Code You can embed PHP into HTML using the <?php ?> tags. Inside these PHP tags, you can write any PHP code that interacts with the HTML content.
Handle Dynamic Data with PHP Instead of hardcoding static data into your HTML, you can use PHP to insert dynamic content, retrieve data from a database, or handle user interactions.
Example 1: Basic HTML to PHP Conversion (Displaying Dynamic Content)
HTML (Static Content):
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Welcome Page</title>
</head>
<body>
<h1>Welcome to My Website</h1>
<p>Today is a great day!</p>
</body>
</html>
Converted to PHP (Dynamic Content):
php
<?php
// PHP code to get the current date
$currentDate = date("l, F j, Y");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Welcome Page</title>
</head>
<body>
<h1>Welcome to My Website</h1>
<p>Today is <?php echo $currentDate; ?>!</p> <!-- Dynamic content displayed with PHP -->
</body>
</html>
Explanation:
The PHP date() function is used to fetch the current date dynamically.
The PHP code is embedded inside the HTML using <?php ?> tags, and the echo command is used to output the current date into the HTML paragraph.
Example 2: HTML Form to PHP (Handling Form Submissions)
HTML Form (Static Content):
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Contact Form</title>
</head>
<body>
<h1>Contact Us</h1>
<form action="submit_form.php" method="POST">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required><br>
<button type="submit">Submit</button>
</form>
</body>
</html>
PHP Script (submit_form.php):
php
<?php
// Handle form submission and display the entered data
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST["name"];
$email = $_POST["email"];
echo "<h1>Thank you for contacting us, $name!</h1>";
echo "<p>We will reach out to you at $email shortly.</p>";
}
?>
Explanation:
The form in the HTML sends data to the submit_form.php script using the POST method.
In the submit_form.php file, PHP retrieves the data from the form using the $_POST superglobal and then displays a thank-you message.
Example 3: HTML Table to PHP (Fetching Data from a Database)
HTML Table (Static Content):
html
<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<tr>
<td>John Doe</td>
<td>john@example.com</td>
<td>28</td>
</tr>
<tr>
<td>Jane Smith</td>
<td>jane@example.com</td>
<td>34</td>
</tr>
</tbody>
</table>
Converted to PHP (Fetching Data from a MySQL Database):
php
<?php
// PHP code to connect to MySQL database and fetch data
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "my_database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Query to fetch data from the database
$sql = "SELECT name, email, age FROM users";
$result = $conn->query($sql);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Users List</title>
</head>
<body>
<h1>Users List</h1>
<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<?php
// Loop through the database result and display each user
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["name"] . "</td><td>" . $row["email"] . "</td><td>" . $row["age"] . "</td></tr>";
}
} else {
echo "<tr><td colspan='3'>No records found</td></tr>";
}
?>
</tbody>
</table>
</body>
</html>
<?php
// Close connection
$conn->close();
?>
Explanation:
The PHP code at the top connects to a MySQL database (my_database) and fetches data from the users table.
The table is populated dynamically with the fetched data from the database using a while loop.
If there are no records, a message is displayed instead of the rows.
Example 4: Including Reusable Code (Header and Footer)
HTML Code (Static):
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Website</title>
</head>
<body>
<header>
<h1>Welcome to My Website</h1>
</header>
<main>
<p>This is the main content.</p>
</main>
<footer>
<p>© 2025 My Website</p>
</footer>
</body>
</html>
Converted to PHP with Included Header and Footer:
header.php:
php
<header>
<h1>Welcome to My Website</h1>
</header>
footer.php:
php
<footer>
<p>© 2025 My Website</p>
</footer>
index.php:
php
<?php include 'header.php'; ?>
<main>
<p>This is the main content.</p>
</main>
<?php include 'footer.php'; ?>
Explanation:
Instead of duplicating the header and footer in each page, you can use PHP include to include common code such as a header and footer across multiple pages.
This makes the code more maintainable because if you need to change the header or footer, you only need to do it in one file (header.php or footer.php), and it will automatically reflect on all pages that include them.
Benefits of Converting HTML to PHP:
Dynamic Content Generation: With PHP, you can generate content dynamically based on databases, user input, or external APIs, making your website more interactive.
Separation of Concerns: PHP allows for a clear separation between the HTML (structure) and server-side logic (content generation, database interaction).
Code Reusability: Using PHP includes, functions, and libraries allows for reusing code and making your website more modular and maintainable.
User Interaction: PHP can handle user interaction (like forms or login systems), allowing you to store, process, and display data based on user actions.
Conclusion:
Converting HTML to PHP allows you to make static web pages dynamic by integrating server-side logic, database interaction, and data generation. PHP can transform static content into dynamic, interactive, and database-driven websites. Whether you're handling user inputs, fetching data from a database, or including common components across pages, PHP makes it easy to create a dynamic web experience.