What best practices should be followed when separating PHP code from HTML output in a web development project?

Separating PHP code from HTML output in a web development project helps to improve code readability, maintainability, and scalability. One best practice is to use a templating engine like Twig or Blade to keep the PHP logic separate from the presentation layer. Another approach is to use MVC (Model-View-Controller) architecture to separate the business logic (Model) from the presentation (View) and user input (Controller).

<?php
// Example using Twig templating engine
require_once 'vendor/autoload.php';

$loader = new \Twig\Loader\FilesystemLoader('templates');
$twig = new \Twig\Environment($loader);

$data = [
    'title' => 'Hello World',
    'content' => 'This is a sample content'
];

echo $twig->render('index.html', $data);
?>