Are there any best practices for structuring PHP code to ensure consistent output in table layouts?

When structuring PHP code for consistent output in table layouts, it's important to use a systematic approach to generate the HTML table structure. One effective way to achieve this is by separating the PHP logic from the HTML markup, using loops to iterate over data and generate table rows dynamically. Additionally, utilizing CSS classes for styling and ensuring proper indentation can help maintain a clean and organized table layout.

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

// Output table
echo '<table>';
echo '<tr>';
foreach(array_keys($data[0]) as $key) {
    echo '<th>' . $key . '</th>';
}
echo '</tr>';

foreach($data as $row) {
    echo '<tr>';
    foreach($row as $value) {
        echo '<td>' . $value . '</td>';
    }
    echo '</tr>';
}
echo '</table>';
?>