How can PHP be used to dynamically generate content for headers and footers on web pages?

To dynamically generate content for headers and footers on web pages using PHP, you can create separate header.php and footer.php files with the desired content. Then, include these files in your main PHP files using the include() or require() functions. This way, any changes made to the header or footer files will automatically reflect on all pages where they are included.

// header.php
<!DOCTYPE html>
<html>
<head>
    <title>Dynamic Header Example</title>
</head>
<body>
    <header>
        <h1>Welcome to our website!</h1>
    </header>

// footer.php
    <footer>
        <p>© 2021 Your Website</p>
    </footer>
</body>
</html>

// index.php
<?php
include 'header.php';
?>
<main>
    <p>Main content of the page goes here.</p>
</main>
<?php
include 'footer.php';
?>