What are some best practices for organizing PHP code within an HTML document to improve readability and maintainability?

To improve readability and maintainability of PHP code within an HTML document, it is recommended to separate PHP logic from HTML markup by using a templating engine like Twig or Blade. This helps to keep the code organized and makes it easier to understand and maintain. Additionally, using proper indentation, comments, and naming conventions can also enhance the readability of the code.

<?php
// Separate PHP logic from HTML markup using a templating engine like Twig
// This helps to keep the code organized and makes it easier to understand and maintain
// Example using Twig template engine:
$loader = new \Twig\Loader\FilesystemLoader('path/to/templates');
$twig = new \Twig\Environment($loader);

echo $twig->render('index.html', ['variable' => $value]);
?>