In PHP development, handling strings is a frequent task, and measuring string length is a key part of it. PHP offers several ways to calculate string length, with strlen() and mb_strlen() being the most commonly used functions. This article explores both functions in detail, explaining their usage and differences to help developers choose the right one based on context.
strlen() is a built-in PHP function that returns the byte length of a string. It works well for English characters or UTF-8 strings that don’t contain special characters.
$str = 'Hello world!';
$length = strlen($str);
echo $length;
Output:
12
It's important to note that strlen() calculates the number of bytes, not characters. So when working with Chinese or other multibyte characters, the result may be inaccurate. Using this function in such scenarios can lead to logic errors.
mb_strlen() is part of PHP's multibyte string functions. It accurately counts characters in multibyte-encoded strings like UTF-8, making it ideal for Chinese, Japanese, and other non-ASCII languages.
$str = '你好,世界!';
$length = mb_strlen($str, 'UTF-8');
echo $length;
Output:
7
mb_strlen() accepts two parameters:
While mb_strlen() handles multibyte strings correctly, it may be slightly less efficient than strlen() when dealing with simple ASCII strings. For performance-sensitive cases without multibyte characters, strlen() might be the better choice.
The core difference lies in how they interpret strings:
If your strings include non-English characters, using mb_strlen() is highly recommended for accurate results.
Choosing the right string length function in PHP is crucial for reliable code. Use strlen() for pure English or single-byte strings, and mb_strlen() for any multibyte scenarios. Understanding when and how to use each function will help ensure your string handling logic is robust and encoding-aware.