What are some methods to dynamically extend an existing HTML table using PHP?

When needing to dynamically extend an existing HTML table using PHP, one approach is to store the table data in an array and then loop through the array to generate the table rows. This allows for easy addition of new data to the array which will automatically reflect in the table when the page is loaded.

<?php
// Sample array of table data
$tableData = [
    ["Name" => "John Doe", "Age" => 25, "Location" => "New York"],
    ["Name" => "Jane Smith", "Age" => 30, "Location" => "Los Angeles"],
    // Add more data as needed
];

// Start the HTML table
echo "<table border='1'>";
// Output table headers
echo "<tr>";
foreach(array_keys($tableData[0]) as $header) {
    echo "<th>$header</th>";
}
echo "</tr>";

// Output table rows
foreach($tableData as $row) {
    echo "<tr>";
    foreach($row as $value) {
        echo "<td>$value</td>";
    }
    echo "</tr>";
}

// End the HTML table
echo "</table>";
?>