What are some best practices for separating PHP processing from HTML output to avoid issues like the footer being misplaced?

When PHP processing is mixed with HTML output, it can lead to issues like misplaced elements such as the footer. To avoid this, it's best practice to separate PHP processing logic from the HTML output by using PHP opening and closing tags within the HTML code.

<?php
// PHP processing logic here
$footerText = "Copyright © 2022 MyWebsite.com";

// HTML output with PHP variables
?>
<!DOCTYPE html>
<html>
<head>
    <title>My Website</title>
</head>
<body>
    <header>
        <h1>Welcome to My Website</h1>
    </header>
    
    <main>
        <p>This is the main content of the website.</p>
    </main>
    
    <footer>
        <p><?php echo $footerText; ?></p>
    </footer>
</body>
</html>