What are best practices for structuring PHP code to efficiently handle and output database query results in a tabular format?

When handling and outputting database query results in a tabular format in PHP, it is best practice to use a combination of HTML and PHP to structure the data efficiently. One way to achieve this is by looping through the query results and dynamically generating the table rows and columns. This approach allows for flexibility in displaying data and ensures a clean and organized presentation of the database information.

<?php
// Assuming $results is the array of database query results

echo "<table>";
echo "<tr><th>ID</th><th>Name</th><th>Email</th></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>";
?>