What are some best practices for escaping characters in PHP to ensure security?
When outputting user-generated content in PHP, it is important to escape characters to prevent cross-site scripting (XSS) attacks. One common method is to use the htmlspecialchars function to convert special characters to HTML entities. This ensures that any potentially harmful code is displayed as text rather than being executed.
$user_input = "<script>alert('XSS attack');</script>";
$escaped_input = htmlspecialchars($user_input, ENT_QUOTES, 'UTF-8');
echo $escaped_input;