What is the purpose of using htmlspecialchars in PHP forms?

When displaying user input on a webpage, it is important to sanitize the input to prevent cross-site scripting (XSS) attacks. One way to do this in PHP forms is by using the htmlspecialchars function. This function converts special characters like < and > into their HTML entities, making it safe to display user input on the webpage without executing any potentially harmful scripts.

// Sanitize user input using htmlspecialchars
$user_input = htmlspecialchars($_POST[&#039;user_input&#039;]);
echo &quot;User input: &quot; . $user_input;