Current Location: Home> Latest Articles> Understanding strlen vs mb_strlen in PHP for String Length Calculation

Understanding strlen vs mb_strlen in PHP for String Length Calculation

gitbox 2025-06-27

Introduction

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.

The strlen() Function

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.

How to Use strlen()


$str = 'Hello world!';
$length = strlen($str);
echo $length;

Output:

12

Important Notes on strlen()

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.

The mb_strlen() Function

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.

How to Use mb_strlen()


$str = '你好,世界!';
$length = mb_strlen($str, 'UTF-8');
echo $length;

Output:

7

Parameters of mb_strlen()

mb_strlen() accepts two parameters:

  • The first is the string whose length you want to calculate.
  • The second is optional and defines the string's encoding. If not specified, it uses the encoding returned by mb_internal_encoding().

Important Notes on mb_strlen()

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.

Differences Between strlen() and mb_strlen()

The core difference lies in how they interpret strings:

  • strlen() returns the byte length and is best for ASCII or basic UTF-8 strings.
  • mb_strlen() returns the actual character count, making it suitable for multibyte encodings like UTF-8 with Chinese or Japanese characters.

If your strings include non-English characters, using mb_strlen() is highly recommended for accurate results.

Conclusion

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.