strlen($string) returns the length of a string in bytes. For characters, which is what you want for Japanese text, accented letters and emoji, use mb_strlen($string). For plain English text the two give the same number.
Spaces, punctuation and newlines all count. strlen("a b\n") is 4.
Bytes vs characters in UTF-8
PHP strings are sequences of bytes, and UTF-8 uses a different number of bytes per character: 1 for ASCII letters and digits, 2 for letters such as é or ж, 3 for kana, common kanji, Chinese characters and symbols such as €, and 4 for most emoji and rare kanji such as 𠮷. strlen() adds up the bytes; mb_strlen() counts the characters.
Use strlen() when bytes matter: a database column limited in bytes, a file size, a binary protocol. Use mb_strlen() for anything a person reads: a character counter, a "max 20 characters" rule, cutting text with mb_substr().
Emoji and characters made of several code points
mb_strlen() counts Unicode code points. Some things a reader sees as one character are several code points joined together: a family emoji, a flag, a skin tone, or a letter plus a combining accent.
Counting what a reader sees as single characters (grapheme clusters) needs grapheme_strlen() from the intl extension:
echo grapheme_strlen('👨👩👧'); // 1
echo grapheme_strlen('🇯🇵'); // 1
For a character counter on a Japanese form, mb_strlen() is usually enough: every kana and kanji is a single code point.
Display width with mb_strwidth
On a terminal or in a fixed-width layout, a full-width Japanese character takes two columns while a half-width katakana or Latin letter takes one. mb_strwidth() returns that width, which is what Japanese "全角は2文字" rules usually mean.
Check if a string is empty
The clearest test is a strict comparison with ''. strlen($s) === 0 gives the same answer. empty($s) is not the same: it also returns true for the string "0", which is a valid answer in many forms.
A string of spaces is not empty. Run it through trim() first if spaces alone should count as nothing.
strlen(null) and non-strings
strlen() expects a string. Passing null still returns 0, but since PHP 8.1 it prints a deprecation notice:
Deprecated: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in /home/index.php on line 2
An array throws TypeError: strlen(): Argument #1 ($string) must be of type string, array given.
Cast or default the value first, for example strlen($value ?? ''). Integers are converted to strings, so strlen(12345) is 5. To count array elements, use count().
Validate the length of user input
A typical rule: a username of 3 to 16 characters, after trimming. Use mb_strlen() so a Japanese name is measured in characters, not bytes.
With strlen() in place of mb_strlen(), やまだたろう would count as 18 and be rejected as too long, even though it is six characters.
Frequently Asked Questions
How do I get the length of a string in PHP?
strlen($s) returns the number of bytes and mb_strlen($s) the number of characters. For plain English text they are equal; for Japanese, accented letters or emoji use mb_strlen().
Why does strlen() return the wrong length for Japanese text?
It is not wrong, it counts bytes. UTF-8 stores kana and common kanji in 3 bytes each, so strlen('日本語') is 9 while mb_strlen('日本語') is 3.
How do I count the characters of a string with emoji?
mb_strlen() counts Unicode code points, so a simple emoji counts as 1, but an emoji built from several code points (a family, a flag, a skin tone) counts as more. grapheme_strlen() from the intl extension counts what a reader sees as one character.
What is the difference between strlen and count in PHP?
strlen() measures a string, count() counts the elements of an array. Passing an array to strlen() throws a TypeError in PHP 8.
How do I check if a string is empty in PHP?
Compare it with an empty string: $s === '', or strlen($s) === 0. Avoid empty($s) for this, because it also treats the string "0" as empty.