What potential issues may arise when trying to output a variable using the alt attribute in PHP?

When trying to output a variable using the alt attribute in PHP, the variable may not be properly escaped, leading to potential security vulnerabilities such as cross-site scripting (XSS) attacks. To solve this issue, you should always sanitize and escape the variable before outputting it in the alt attribute to prevent any malicious code injection.

<?php
// Sanitize and escape the variable before outputting it in the alt attribute
$variable = htmlspecialchars($variable, ENT_QUOTES, 'UTF-8');
echo "<img src='image.jpg' alt='$variable'>";
?>