What potential issues can arise when trying to format a variable for output in a table in PHP?

When trying to format a variable for output in a table in PHP, potential issues can arise if the variable contains special characters or HTML tags that could break the table structure or cause unintended behavior. To avoid this, you can use the htmlspecialchars() function to escape special characters and prevent HTML injection.

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