What is the significance of using htmlspecialchars() when outputting user input in PHP?
When outputting user input in PHP, it is important to use htmlspecialchars() to prevent cross-site scripting (XSS) attacks. This function converts special characters like < and > into their HTML entity equivalents, ensuring that user input is displayed as text and not executed as code. By using htmlspecialchars(), you can protect your application from malicious scripts that could be injected through user input.
$userInput = "<script>alert('XSS attack!')</script>";
echo htmlspecialchars($userInput);