What are common pitfalls when integrating PHP code into HTML tables?

Common pitfalls when integrating PHP code into HTML tables include mixing PHP logic with HTML markup, not properly escaping PHP variables to prevent XSS attacks, and not handling errors or empty data gracefully. To solve these issues, separate PHP logic from HTML markup using loops or conditionals, always sanitize and escape PHP variables before outputting them in HTML, and use error handling techniques such as try-catch blocks.

<?php
// Example of separating PHP logic from HTML markup using loops
$data = array("John", "Doe", "Jane", "Smith");

echo "<table>";
for ($i = 0; $i < count($data); $i += 2) {
    echo "<tr><td>" . htmlspecialchars($data[$i]) . "</td><td>" . htmlspecialchars($data[$i + 1]) . "</td></tr>";
}
echo "</table>";
?>