Have you ever seen text like "%20" in a web address, or strange symbols where an accented letter should be? These are signs of encoding at work. Encoding is the process of converting information from one representation to another so that it can be stored, sent or displayed safely. On the web, several encodings exist side by side, and each one solves a different problem. Knowing the differences will save you hours of debugging.
Why Encoding Exists
Computers store everything as numbers. To represent text, they need an agreement about which number stands for which character. Different systems also have special characters that mean something specific, such as the question mark in a web address or the less-than sign in HTML. When data travels between systems, encoding makes sure it arrives intact and is not mistaken for something else.
Character Encoding: ASCII and UTF-8
ASCII was one of the first standards. It uses 7 bits per character and covers English letters, digits and basic punctuation, 128 characters in total. It cannot represent accented letters, non-Latin scripts or emojis.
Unicode is a universal standard that assigns a unique number, called a code point, to characters from nearly all writing systems and many symbols. UTF-8 is the most common way to store Unicode text. It uses between one and four bytes per character:
- Basic English characters use one byte, and are identical to ASCII.
- Many accented Latin, Greek, Cyrillic, Arabic and Hebrew characters use two bytes.
- Most Chinese, Japanese and Korean characters use three bytes.
- Emojis and rarer symbols use four bytes.
UTF-8 is the dominant encoding for web pages, and using it consistently avoids most problems. A typical bug is called mojibake, where text such as an accented name turns into nonsense characters because it was saved in one encoding and read in another. To prevent this, save files as UTF-8, declare it in your HTML head, and make sure your database and server use it too.
URL Encoding (Percent Encoding)
A web address can safely contain only a limited set of characters. Others must be converted into a percent sign followed by two hexadecimal digits representing the byte value. A space becomes %20, and in form data it is sometimes written as a plus sign. A slash, question mark or ampersand has a special meaning in URLs, so when you want these as ordinary data inside a value, they must be encoded.
Examples:
- A space becomes %20.
- An ampersand becomes %26.
- An accented letter such as e with an acute accent is first converted to UTF-8 bytes, and each byte is then percent-encoded.
When to use it: whenever you build a link or query string containing user input. In PHP, functions such as urlencode and rawurlencode exist for this. In JavaScript, use encodeURIComponent for values.
HTML Entities
HTML uses certain characters for its own syntax, especially the less-than sign, the greater-than sign and the ampersand. If you want to show them as text on a page, you write them as entities: for example < for less-than, > for greater-than and & for the ampersand. Quotation marks can be written as ".
This is also a key security practice. When a page displays text submitted by users, failing to escape these characters can allow attackers to inject scripts into your page, a vulnerability known as cross-site scripting, or XSS. Always escape output according to the context where it will appear. In PHP, htmlspecialchars is the standard function for HTML output.
Base64 Encoding
Base64 represents binary data using 64 safe printable characters. It increases the size by roughly one third. It is used to embed small images in web pages, to put binary content in emails and to carry data inside text-based formats like JSON. It is not encryption, and anyone can decode it.
Quick Comparison
- UTF-8 solves: how to store the characters of all languages as bytes.
- URL encoding solves: how to put arbitrary text inside a web address safely.
- HTML entities solve: how to display reserved characters in a web page without breaking it or creating security holes.
- Base64 solves: how to move binary data through text-only channels.
Double Encoding and Other Common Bugs
One of the most common mistakes is encoding the same data twice. For example, a space encoded once becomes %20, and encoding again turns the percent sign into %25, producing %2520. The receiving side then sees the wrong text. The opposite happens when data is decoded too many times.
Other frequent issues:
- Mixed encodings in one project: a database set to one encoding and a page set to another.
- Forgetting to encode user-provided values in links, which breaks addresses that contain spaces or ampersands.
- Encoding for the wrong context. HTML escaping is not the same as URL encoding, and neither protects database queries. Use prepared statements for databases.
- Copying text from word processors that contains curly quotes, long dashes or non-breaking spaces that later cause failed comparisons.
A Simple Checklist
- Use UTF-8 everywhere: files, HTML, database and server headers.
- Encode values when placing them in URLs.
- Escape output when placing user text in HTML.
- Use Base64 only for moving binary data through text, and never as a security measure.
- Encode once, at the point of use, and decode once at the receiving end.
Try Base64 Yourself
To see how Base64 turns text or files into a string and back, use our free Base64 Encoder and Decoder. It handles text and files directly in your browser, so nothing is sent to a server.
Final Thoughts
Encodings are not complicated once you ask what problem each one solves. UTF-8 stores text, URL encoding protects addresses, HTML entities protect pages and Base64 carries binary data. Match the right encoding to the right situation and encode exactly once.