How can using htmlspecialchars() in PHP make code more universal and prevent the need to modify data in the database?

Using htmlspecialchars() in PHP can make code more universal by encoding special characters in user input, preventing potential security vulnerabilities like cross-site scripting (XSS) attacks. By using htmlspecialchars(), data can be safely outputted to HTML without the need to modify the data stored in the database. This ensures that data remains intact and secure, regardless of where it is displayed.

// Example of using htmlspecialchars() to prevent XSS attacks
$user_input = "<script>alert('XSS attack!');</script>";
$encoded_input = htmlspecialchars($user_input);
echo $encoded_input;