How it works
& → & < → <In HTML, < starts a tag and & starts an entity, so to show those characters as text you write them as entities: < and &. Inside attributes, quotes need the same treatment. Any character can also be written by number, as é (decimal) or é (hex), and many have names, like é or ©.
-
Always escape these five
& → & < → < > → > " → " ' → ' -
Any character by number
é = U+00E9 = 233 → é or é -
Escape & first when doing it by hand
< → &lt; (not <)
Common entities
| Character | Name | Number |
|---|---|---|
| & | & | & |
| < | < | < |
| > | > | > |
| " | " | " |
| ' | ' | ' |
| no-break space | |   |
| © | © | © |
| ® | ® | ® |
| ™ | ™ | ™ |
| € | € | € |
| ° | ° | ° |
| × | × | × |
| — | — | — |
| … | … | … |
Good to know
- Escaping what users type before showing it is the main defense against cross-site scripting (XSS).
- PHP’s htmlspecialchars() and the default setting here escape the same five characters.