What are some best practices for organizing PHP code to avoid layout problems in HTML output?

When organizing PHP code to avoid layout problems in HTML output, it is best to separate PHP logic from HTML markup by using a template system like PHP's built-in `include` function or a more robust templating engine like Twig. This separation of concerns makes it easier to maintain and update the codebase without affecting the layout of the HTML output.

<?php
// header.php
?>
<!DOCTYPE html>
<html>
<head>
    <title>My Website</title>
</head>
<body>

<?php
// index.php
include 'header.php';
?>

<h1>Welcome to my website!</h1>

<?php
// footer.php
include 'footer.php';
?>
</body>
</html>