How can PHP be used to maintain a consistent website layout across multiple pages?
To maintain a consistent website layout across multiple pages, you can use PHP to create a template file that contains the common elements of your website layout, such as header, footer, and navigation. Then, you can include this template file in each of your individual pages using PHP's include or require function. This way, any changes made to the template file will automatically reflect across all pages, ensuring a consistent layout throughout the website.
<?php
// header.php
?>
<!DOCTYPE html>
<html>
<head>
<title>My Website</title>
</head>
<body>
<header>
<h1>Welcome to My Website</h1>
</header>
// footer.php
?>
<footer>
<p>&copy; 2021 My Website</p>
</footer>
</body>
</html>
// index.php
<?php
include 'header.php';
?>
<main>
<p>This is the homepage content.</p>
</main>
<?php
include 'footer.php';
?>
// about.php
<?php
include 'header.php';
?>
<main>
<p>About Us</p>
</main>
<?php
include 'footer.php';
?>