How can developers effectively protect against XSS attacks in PHP applications, beyond using htmlspecialchars?

To effectively protect against XSS attacks in PHP applications beyond using htmlspecialchars, developers can utilize functions like htmlentities or strip_tags to further sanitize user input. Additionally, implementing Content Security Policy (CSP) headers can help prevent malicious scripts from executing on the client-side.

// Example code snippet using htmlentities to further sanitize user input
$unsafe_input = $_POST['user_input'];
$safe_input = htmlentities($unsafe_input, ENT_QUOTES, 'UTF-8');
echo $safe_input;