How can user input be securely processed in PHP to prevent XSS attacks?

To securely process user input in PHP to prevent XSS attacks, you should use functions like htmlspecialchars() or htmlentities() to escape special characters in the input before displaying it on a webpage. This will prevent malicious scripts from being executed in the user input.

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