What are the best practices for separating presentation (HTML) and logic (PHP) in web development?

Separating presentation (HTML) and logic (PHP) in web development is important for maintaining clean and organized code, improving code reusability, and enhancing collaboration among developers. One common approach is to use a templating system like Twig or Blade to separate the presentation layer from the logic layer. This allows developers to focus on writing clean, readable HTML code for the presentation layer and PHP code for the logic layer.

// Example of separating presentation (HTML) and logic (PHP) using Twig templating engine

// Include the Twig autoloader
require_once 'vendor/autoload.php';

// Specify the location of Twig templates
$loader = new \Twig\Loader\FilesystemLoader('templates');

// Initialize Twig environment
$twig = new \Twig\Environment($loader);

// Define variables for the template
$variables = [
    'title' => 'Welcome to my website',
    'content' => 'This is some sample content for the page',
];

// Render the template
echo $twig->render('index.html', $variables);