How can PHP loops and arrays be utilized to streamline table creation and data output in PHP?

To streamline table creation and data output in PHP, we can utilize loops and arrays. By storing our data in arrays and using loops to iterate over them, we can dynamically generate table rows and cells. This approach reduces redundancy in our code and makes it easier to manage and update the data being displayed.

<?php
// Sample data stored in an array
$data = [
    ['Name', 'Age', 'Country'],
    ['John', 25, 'USA'],
    ['Alice', 30, 'Canada'],
    ['Bob', 22, 'UK']
];

// Outputting data in a table using loops
echo '<table border="1">';
foreach ($data as $row) {
    echo '<tr>';
    foreach ($row as $cell) {
        echo '<td>' . $cell . '</td>';
    }
    echo '</tr>';
}
echo '</table>';
?>