How can you optimize the process of generating a table and populating it with data in PHP?

When generating a table and populating it with data in PHP, you can optimize the process by using a loop to iterate through your data and dynamically generate the table rows. This way, you can avoid repetitive code and make your code more efficient.

<?php
// Sample data array
$data = [
    ['Name' => 'John', 'Age' => 25],
    ['Name' => 'Jane', 'Age' => 30],
    ['Name' => 'Mike', 'Age' => 35]
];

// Start table
echo '<table>';
// Add table header
echo '<tr><th>Name</th><th>Age</th></tr>';
// Loop through data and populate table rows
foreach($data as $row){
    echo '<tr>';
    echo '<td>'.$row['Name'].'</td>';
    echo '<td>'.$row['Age'].'</td>';
    echo '</tr>';
}
// End table
echo '</table>';
?>