Why must the ampersand (&) always be escaped first?
Because HTML entities begin with an ampersand; if you escape other characters first and then escape ampersands, you double-escape all previous entities.
Convert special characters to and from HTML entities.
Converts reserved HTML characters into named entity references (&, <, >) and decimal/hex character codes to prevent XSS attacks and rendering bugs.
HTML_Entities = Replace_Characters({ '<': '<', '>': '>', '&': '&', '"': '"', "'": ''' })
Ensures reserved markup delimiters are rendered literally by web browsers rather than executed as DOM markup.
Inputs: Raw String: '<script>alert("XSS");</script>'
Summary: Escaped HTML Entities | XSS Injection Prevented | Browser Displays Literal Text
Because HTML entities begin with an ampersand; if you escape other characters first and then escape ampersands, you double-escape all previous entities.
& (&), < (<), > (>), " ("), and ' (' or ').
Named entities use readable names (©); numeric entities use decimal (©) or hex (©) Unicode code points.