If you work across JavaScript, PHP and Python, “string length” sounds simple until Unicode, emojis and byte length enter the picture. This guide shows the practical difference between character count and byte count, with examples you can copy.
Need a quick manual check? Use the online string length counter to compare characters, words and bytes before writing validation rules.
The short version
For normal text, these are the functions developers usually reach for:
// JavaScript
const text = 'hello';
console.log(text.length); // 5
// PHP
$text = 'hello';
echo strlen($text); // 5 bytes for plain ASCII
# Python
text = 'hello'
print(len(text)) # 5 characters
That works for simple ASCII text. The important detail is that not all languages count the same thing by default.
JavaScript string length
JavaScript uses the .length property:
const username = 'wilson';
if (username.length > 20) {
throw new Error('Username is too long');
}
For simple letters and numbers, .length is usually what you expect. But JavaScript counts UTF-16 code units, not always visible user-perceived characters.
console.log('abc'.length); // 3
console.log('é'.length); // often 1
console.log('😀'.length); // 2
If you need a more user-friendly count for emojis and combined characters, use Array.from() as a better baseline:
const text = '😀abc';
console.log(text.length); // 5
console.log(Array.from(text).length); // 4
PHP string length
In PHP, strlen() counts bytes:
$text = 'hello';
echo strlen($text); // 5
For UTF-8 text, that can be misleading:
echo strlen('á'); // 2 in UTF-8
For human-readable character length, use mb_strlen():
$text = 'Olá mundo';
echo mb_strlen($text, 'UTF-8');
This matters in Laravel validation too. Laravel's max rule for strings is character-oriented, so do not blindly compare it with raw strlen() when dealing with multilingual input.
Python string length
Python's len() counts Unicode code points in a string:
text = 'Olá mundo'
print(len(text))
If you need byte length, encode the string first:
text = 'Olá'
print(len(text)) # 3 characters
print(len(text.encode('utf-8'))) # 4 bytes
This distinction is useful when preparing data for APIs, databases, file formats or protocols with byte limits.
Character count vs byte length
A character count answers: “How many text elements are in this string?”
A byte length answers: “How much storage or payload size does this encoded string use?”
Example:
Text: Olá
Characters: 3
UTF-8 bytes: 4
Use character length for:
- form validation
- usernames and titles
- article excerpts
- UI limits
Use byte length for:
- storage limits
- network payload limits
- binary protocols
- low-level file handling
Quick comparison
// JavaScript byte length in UTF-8
const bytes = new TextEncoder().encode('Olá').length;
console.log(bytes);
// PHP characters vs bytes
$text = 'Olá';
echo mb_strlen($text, 'UTF-8'); // characters
echo strlen($text); // bytes
# Python characters vs bytes
text = 'Olá'
print(len(text))
print(len(text.encode('utf-8')))
Practical validation tip
When validating user input, decide first what the limit means:
- If it is a user-facing limit, count characters.
- If it is a storage or API limit, count bytes.
- If emojis matter, test with real emoji examples.
You can paste sample values into the string length counter to quickly compare what users see with what your backend stores.
FAQ
Is JavaScript .length the real character count?
Not always. It counts UTF-16 code units. For many strings it matches the visible character count, but emojis and some combined Unicode characters can differ.
Should I use strlen() or mb_strlen() in PHP?
Use strlen() for bytes. Use mb_strlen() for readable text length in UTF-8 strings.
Does Python len() count bytes?
No. On a string, len() counts characters/code points. To count bytes, encode the string first, for example with text.encode('utf-8').
Why do emojis break length checks?
Many emojis are represented by multiple code units or code points. A user sees one symbol, but the programming language may count more than one unit internally.
