What are best practices for structuring PHP code to display database results in a table format?

When displaying database results in a table format in PHP, it is best practice to separate your HTML markup from your PHP logic to keep your code clean and maintainable. One way to achieve this is by using a loop to iterate over the database results and generate the table rows dynamically. Additionally, consider using CSS classes to style the table for better presentation.

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

echo '<table>';
echo '<tr>';
echo '<th>ID</th>';
echo '<th>Name</th>';
echo '<th>Email</th>';
echo '</tr>';

foreach ($results as $row) {
    echo '<tr>';
    echo '<td>' . $row['id'] . '</td>';
    echo '<td>' . $row['name'] . '</td>';
    echo '<td>' . $row['email'] . '</td>';
    echo '</tr>';
}

echo '</table>';
?>