Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

strlen

Returns the length of a string in bytes.

Available since: PHP 4

Syntax

strlen(string $string): int

Parameters

ParameterTypeDescription
$stringstringThe string to measure.

Return value

intThe number of bytes in the string; 0 for an empty string.

Examples

Length of an ASCII string:

<?php
 
echo strlen('hello');

Output:

5

A Cyrillic letter takes two bytes in UTF-8, so the result is twice the number of letters:

<?php
 
echo strlen('привет'), "\n";
echo mb_strlen('привет');

Output:

12
6

Notes

The function counts bytes, not characters. To get the number of characters in non-ASCII text, use mb_strlen().

See also

  • mb_strlen — Returns the number of characters in a string.

Related recipes

In the tutorial

  • Strings — Working with strings in PHP — quotes, concatenation, length, searching, replacing and multibyte functions.

Thanks for reading!

Donate to the site — we work hard for you.