What are some alternative methods to handle XSS attacks in PHP, aside from using htmlentities()?

One alternative method to handle XSS attacks in PHP is to use the htmlspecialchars() function. This function converts special characters to their respective HTML entities, making it safe to output user-generated content on a webpage without risking XSS attacks.

$user_input = "<script>alert('XSS attack!')</script>";
$filtered_input = htmlspecialchars($user_input, ENT_QUOTES, 'UTF-8');
echo $filtered_input;