PHP strlen Function: How It Works with Strings

PHP strlen Function

Developers needed a way to check the length of a string, so the strlen function was introduced to handle this task in PHP.

Understand the strlen() Function in PHP

The strlen() function in PHP tells you how many characters a string has. It counts each byte, not each letter.

Here is the syntax:

strlen( $string )

You pass one string and it gives you a number back. It gives you an integer. That number shows how many bytes the string holds.

PHP counts every byte in the string. It starts from the first character. It stops when it reaches the end.

Here is an example:

echo strlen("Flatcoding");

This prints 5. That string has five letters, and each letter uses one byte.

echo strlen("");

This gives 0. The string has no content, so the count is zero.

Here are the use cases:

  • You check if a field is empty and check if it is too short.
  • It stops weak passwords and makes sure they are not shorter than a safe length.
  • You can cut long strings and check their length first with strlen().

Handle Multibyte Strings (Important Warning)

strlen() does not count letters in multibyte strings. It counts bytes. If a letter uses more than one byte, strlen() gives the wrong length.

To handle this, use mb_strlen().

echo strlen("你好");
echo mb_strlen("你好");

The output:

6
2

Unicode has characters that use two or more bytes. strlen() does not treat them as one unit. That causes problems with length checks or output limits.

The Difference Between strlen() an mb_strlen()

The strlen() counts the number of bytes in a string.

It works well for ASCII strings. Each character takes one byte. But for multibyte characters, like those in UTF-8 (e.g. Arabic, Chinese, or emoji), strlen() counts each byte, not each character.

For example:

$str = "こんにちは";
echo strlen($str); 

Output:

Hello
15

This string has 5 characters, but strlen() returns 15 because each character uses 3 bytes in UTF-8.

While the mb_strlen() counts the number of characters, not bytes.

It works correctly for multibyte strings. You must set the correct encoding. Most of the time, that is UTF-8.

$str = "こんにちは";
echo mb_strlen($str, "UTF-8"); // Output: 5

Now the function shows the correct count: 5 characters.

Examples

Simple check:

$username = "admin";
if (strlen($username) < 4) {
    echo "Username too short";
}

This checks if the username has fewer than four characters. It runs fast and works for basic cases.

Multibyte problem:

$name = "José";
echo strlen($name);      
echo mb_strlen($name);  

strlen($name) returns 5 because strlen counts bytes, and the character é (accented e) is a 2-byte character in UTF-8. So "José" consists of:

  • J → 1 byte
  • o → 1 byte
  • s → 1 byte
  • é → 2 bytes

mb_strlen($name) returns 4 because mb_strlen counts characters, not bytes. It correctly handles multibyte characters like é.

Wrapping Up

In this article, you learned how strlen() counts the bytes in a string. You also saw why it fails with multibyte characters.

Here is a quick recap:

  • Use strlen() for basic strings
  • Use mb_strlen() for multibyte or Unicode strings
  • strlen() counts bytes, not real characters
  • It helps with form checks and limits with trims

Thank you for reading. Click here to see more PHP tutorials.

FAQs

What does strlen() return in PHP?

It returns an integer that shows how many bytes are in the string.

Does strlen() work with multibyte strings?

No. It gives the byte count, not the real number of letters.

What is the difference between strlen() and mb_strlen()?

strlen() counts bytes. mb_strlen() counts characters.

Is strlen() in all PHP versions?

Yes. It came with the first versions and never left.

Why is strlen() not accurate for Unicode?

Unicode characters use more than one byte. strlen() counts each byte, not the full character.

Similar Reads

PHP XML Parsers: Everything You Need to Know

In PHP, manipulation of XML data can be very critical when most systems' data exchange relies on the format. XML—Extensible…

PHP Filters: How to Validate and Sanitize User Inputs

User input can be unpredictable and dangerous. A simple form submission might carry malicious code or invalid data and lead…

PHP mb_substr Function: How it Works with Examples

You may need to get part of a text when you work with text in PHP. The mb_substr gives you…

PHP array_key_last Function: How it Works with Examples

The array_key_last gives you the last key in an array and works with numeric and associative arrays in PHP. It…

OOP Interface PHP: How to Set Rules for Classes

PHP developers relied on class inheritance to share functionality before the OOP interface, but this approach had limits. It solves…

PHP trim(): How to remove Whitespace from String

Early PHP scripts often failed because of extra spaces or line breaks in strings. These issues came from user input…

PHP array_merge: Combine Multiple Arrays into One

You have two or more arrays. You want one. That is the problem array_merge solves in PHP. This function lets…

How to Connect a MySQL Database to PHP?

Connecting PHP to MySQL is not just a technical step—it is the basic part of how data-driven websites work. From…

PHP JSON Handling with json_encode & json_decode

JSON is used in most of your web applications for dealing with data, and so, in a way, PHP JSON…

PHP fopen: Understanding fopen Modes

PHP has great tools to handle external files such as opening, writing, deleting and more else, in this tutorial we…

Previous Article

PHP str_replace Function: How it Works with Examples

Next Article

Assignment Operator in JavaScript with Examples

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.