How can htmlentities() be used effectively in PHP to prevent security vulnerabilities?
Using htmlentities() in PHP can help prevent security vulnerabilities such as cross-site scripting (XSS) attacks by converting potentially harmful characters into their HTML entities. This ensures that user input is displayed as plain text rather than being interpreted as HTML code. By using htmlentities() to sanitize user input before outputting it to the browser, you can protect your application from XSS attacks.
$user_input = "<script>alert('XSS attack!');</script>";
$clean_input = htmlentities($user_input);
echo $clean_input;