How can the separation of logic and presentation be improved in the PHP code to make it more maintainable?

To improve the separation of logic and presentation in PHP code, one can use a template engine like Twig or Blade to separate the HTML presentation from the PHP logic. This allows for cleaner and more maintainable code as the logic and presentation are clearly separated.

// Using Twig template engine for separation of logic and presentation
require_once 'vendor/autoload.php';

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

$products = [
    ['name' => 'Product 1', 'price' => 10],
    ['name' => 'Product 2', 'price' => 20],
    ['name' => 'Product 3', 'price' => 30]
];

echo $twig->render('products.html', ['products' => $products]);