What are some best practices for structuring PHP code to handle table row and column output?

When outputting table rows and columns in PHP, it is best practice to separate the HTML markup from the PHP logic for better readability and maintainability. One way to achieve this is by using a loop to iterate over your data and outputting each row and column dynamically.

<?php
// Sample data
$tableData = [
    ['Name', 'Age', 'City'],
    ['John Doe', 30, 'New York'],
    ['Jane Smith', 25, 'Los Angeles'],
    ['Mike Johnson', 35, 'Chicago']
];

// Output table
echo '<table>';
foreach ($tableData as $row) {
    echo '<tr>';
    foreach ($row as $column) {
        echo '<td>' . $column . '</td>';
    }
    echo '</tr>';
}
echo '</table>';
?>