PHP strlen() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, strlen() returns the length of a string, counted in bytes.

It takes one required argument. For plain English text, bytes and characters are the same, but not for every language.

Syntax

Call strlen() with the values you need. Start with this line.

PHP
$length = strlen($text);
CallWhat it does
strlen("Hello")Returns 5.
strlen("")Returns 0 for an empty string.
strlen("héllo")Returns 6 because "é" uses 2 bytes in UTF-8.

For text that may have accents, emojis, or non-English letters, use mb_strlen() to count characters instead of bytes.

Library declarations
PHP
strlen(string $string): int

Parameters

ParameterDescription
stringRequired. The string whose length you want to measure.

Return Value

Return ValueDescription
intThe number of bytes in the string. An empty string returns 0.

PHP Examples

Example 1 — Count Characters in a String

The simplest use: measure a short word.

PHP
<?php
$word = "Hello";

echo strlen($word), PHP_EOL;

How It Works

  1. Store a word in a variable.
  2. Pass it to strlen.
  3. PHP counts each byte. Here each letter is one byte.
  4. echo prints the result: 5.

Example 2 — Bytes vs Characters

Accented letters use more than one byte in UTF-8.

PHP
<?php
$word = "héllo";

echo strlen($word), PHP_EOL;
echo mb_strlen($word), PHP_EOL;

How It Works

  1. Use a word that contains the letter é.
  2. strlen counts bytes. The é takes two bytes, so the answer is 6.
  3. mb_strlen counts characters, so it returns 5.
  4. Pick mb_strlen when you want what the user sees.

Example 3 — Check Minimum Password Length

A common real-world use: validate input length.

PHP
<?php
$password = "secret12";

if (strlen($password) >= 8) {
    echo "Password length is OK", PHP_EOL;
} else {
    echo "Password is too short", PHP_EOL;
}

echo strlen(""), PHP_EOL;

How It Works

  1. Store the password in a variable.
  2. Compare strlen with the minimum length of 8.
  3. The password has exactly 8 bytes, so the check passes.
  4. An empty string has length 0, which is also a handy check.

Frequently Asked Questions

strlen() returns the number of bytes in a string. For simple English text, that is the same as the number of characters.
PHP strings are byte arrays. In UTF-8, letters like é or emojis take 2 to 4 bytes, and strlen() counts every byte.
Use mb_strlen($string, 'UTF-8'). It counts real characters and is the right choice for multi-language text.
It returns 0. You can use strlen($text) === 0 to test for an empty string.
Passing an array throws a TypeError. Passing null is deprecated since PHP 8.1, so convert it to a string first, for example with (string) $value.

Next: strnatcasecmp()

strnatcasecmp() compares two strings using natural order, ignoring case.

strnatcasecmp() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful