What are some potential pitfalls when using PHP to output data in HTML tables?

One potential pitfall when using PHP to output data in HTML tables is not properly escaping the data, which can lead to security vulnerabilities such as cross-site scripting (XSS) attacks. To prevent this, it is important to use functions like htmlspecialchars() to escape the data before outputting it in the table.

<?php
$data = "<script>alert('XSS attack!')</script>";
echo "<table>";
echo "<tr><td>" . htmlspecialchars($data) . "</td></tr>";
echo "</table>";
?>