Are there any potential pitfalls when dynamically generating HTML tables with PHP within a loop?

One potential pitfall when dynamically generating HTML tables with PHP within a loop is not properly escaping the data being inserted into the table cells. This can lead to security vulnerabilities such as cross-site scripting (XSS) attacks. To solve this issue, make sure to use functions like htmlspecialchars() to escape the data before outputting it in the table.

<?php
// Sample data
$data = [
    ['John', 'Doe', 'john.doe@example.com'],
    ['Jane', 'Smith', 'jane.smith@example.com']
];

// Outputting HTML table with escaped data
echo '<table>';
foreach ($data as $row) {
    echo '<tr>';
    foreach ($row as $cell) {
        echo '<td>' . htmlspecialchars($cell) . '</td>';
    }
    echo '</tr>';
}
echo '</table>';
?>