strlen
Returns the number of bytes in a string, not the number of characters.
strlen(string $string): int- Added in PHP 4.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$string | string | The string being measured for length. |
Return value
intThe length of the string in bytes.
strlen() returns how many bytes a string occupies. For plain ASCII text that is the same as the number of characters, which is why the difference goes unnoticed until the first accented letter or emoji arrives.
When to use it
Reach for strlen() when the thing you care about is really size in bytes: checking a payload against a column limit, validating a fixed-width record, computing an offset for substr(), or looping over a binary buffer. It is the correct tool for protocol and storage work.
For anything a human will read as "how many letters", use mb_strlen() instead. In UTF-8, é is two bytes, most CJK characters are three, and an emoji is four or more — so strlen('héllo') is 6, not 5.
Emptiness checks
Use $s === '' to test for an empty string rather than strlen($s) === 0; both work, but the comparison is clearer. Avoid empty($s) when '0' is a legitimate value — empty('0') is true, while strlen('0') is 1.
Unlike C, PHP strings are binary safe, so a null byte is counted like any other byte rather than ending the string.
Example
When to use it
- Check that a value fits a fixed-width database column before inserting it.
- Compute a substring offset, e.g. strlen($prefix) as the start position for substr().
- Validate a fixed-length code such as an ISBN, IBAN or country code.
Frequently asked questions
What is the difference between strlen and mb_strlen?
strlen counts bytes while mb_strlen counts characters in a given encoding. They agree for ASCII text but diverge as soon as the string contains multibyte characters such as accented letters, CJK characters or emoji.
Does strlen count spaces?
Yes. Spaces, tabs, newlines and null bytes are all ordinary bytes and each adds to the count. Call trim() first if you want to ignore surrounding whitespace.
How do I check if a string is empty in PHP?
Compare it directly with $s === ''. That is clearer than strlen($s) === 0 and, unlike empty($s), it does not treat the string '0' as empty.
More examples
Validating a fixed-length code
The output shows each code with its byte length and a verdict: only AB12 is four bytes, so AB1 and AB123 are rejected. Because the codes are ASCII, byte length and character length agree here.
<?php
$codes = ['AB12', 'AB1', 'AB123'];
foreach ($codes as $code) {
$len = strlen($code);
printf("%-6s %d %s\n", $code, $len, $len === 4 ? 'ok' : 'wrong length');
}strlen versus empty()
The row for '0' is the interesting one: strlen('0') > 0 is true but !empty('0') is false, because '0' is falsy in PHP. If you use empty() to check that a form field was filled in, a user who types 0 will be told the field is blank.
<?php
$values = ['0', '', ' ', 'php'];
foreach ($values as $v) {
printf("%-5s strlen>0: %-5s !empty: %s\n",
"'" . $v . "'",
strlen($v) > 0 ? 'true' : 'false',
!empty($v) ? 'true' : 'false');
}
Discussion