What are the best practices for using ENT_COMPAT or ENT_QUOTE instead of ENT_NOQUOTES in htmlspecialchars() for improved XSS protection in PHP?

When using htmlspecialchars() in PHP to prevent XSS attacks, it is recommended to use ENT_COMPAT or ENT_QUOTES instead of ENT_NOQUOTES for improved protection. This ensures that both single and double quotes are encoded, preventing attackers from injecting malicious scripts into the output.

// Using ENT_COMPAT or ENT_QUOTES instead of ENT_NOQUOTES for improved XSS protection
$unsafe_input = "<script>alert('XSS attack!');</script>";
$safe_output = htmlspecialchars($unsafe_input, ENT_QUOTES);
echo $safe_output;