The five characters that always matter

CharacterNamed entityNumeric entityWhy escape?
&&&Starts every other entity — always first.
<&lt;&#60;Opens HTML tags.
>&gt;&#62;Closes HTML tags (defensive).
"&quot;&#34;Breaks attribute boundaries.
'&#39;&#39;Breaks single-quoted attributes.
!

This is an XSS-prevention primitive, not a complete defense. Escaping is context-sensitive — HTML attribute, JavaScript string, and URL contexts each require different rules. See OWASP's XSS Prevention Cheat Sheet for the full picture, and our reference on Cross-Site Scripting.

FAQ

Named or numeric entities?

Both are valid. Numeric entities (e.g. &#39; for an apostrophe) work everywhere. Named entities (&apos;) are sometimes shorter but a few are unsafe in legacy parsers — &apos; isn't defined in HTML4.

Do I need to encode every Unicode character?

No. Modern HTML files served as UTF-8 can contain any Unicode character directly. Only encode the five HTML-significant characters and any characters that confuse your target charset.