What potential security risks are associated with using htmlspecialchars() in PHP scripts?

Using htmlspecialchars() in PHP scripts can help prevent Cross-Site Scripting (XSS) attacks by converting special characters to their HTML entities. However, it is important to note that using htmlspecialchars() alone may not provide complete protection against all XSS vulnerabilities. It is recommended to also set the Content-Type header to "text/html" to ensure proper rendering of the HTML entities.

<?php
header('Content-Type: text/html; charset=utf-8');
$input = "<script>alert('XSS attack');</script>";
$escaped_input = htmlspecialchars($input, ENT_QUOTES, 'UTF-8');
echo $escaped_input;
?>