Are there any best practices for organizing and structuring PHP code when outputting database results into HTML elements?

When outputting database results into HTML elements in PHP, it is best practice to separate your PHP logic from your HTML markup for better readability and maintainability. One way to achieve this is by using a template engine like Twig or Blade to separate the presentation layer from the business logic. This helps in keeping your code clean and organized, making it easier to update and modify in the future.

<?php
// Assume $results is an array of database results

// Include the template engine library
require_once 'vendor/autoload.php';

// Create a new instance of the template engine
$loader = new \Twig\Loader\FilesystemLoader('templates');
$twig = new \Twig\Environment($loader);

// Render the template with the database results
echo $twig->render('results.html', ['results' => $results]);
?>