Which PHP functions, such as htmlentities(), strip_tags(), or htmlspecialchars(), are most effective in sanitizing user input?

When dealing with user input in PHP, it's important to sanitize the data to prevent security vulnerabilities such as cross-site scripting (XSS) attacks. Functions like htmlentities(), strip_tags(), and htmlspecialchars() are commonly used for this purpose. Of these functions, htmlspecialchars() is typically the most effective as it converts special characters to HTML entities, preventing any malicious code from being executed in the browser.

// Sanitize user input using htmlspecialchars()
$userInput = "<script>alert('XSS attack!');</script>";
$cleanInput = htmlspecialchars($userInput, ENT_QUOTES, 'UTF-8');

echo $cleanInput;