Getting the Length of a String in PHP

To get the length of a string in PHP, use the strlen($string) built-in function. The strlen() takes a string as an argument and returns the length of the string. The strlen() function returns the number of bytes, not characters. If your string contains Unicode characters, strlen() will return a string length bigger than the number of visible characters since Unicode characters are represented in PHP as 2 bytes. To get the number of visible characters, use the mb_strlen($string, 'utf8') function instead of strlen(). Recent versions of PHP have significantly improved the performance of the strlen() function. Multiple strlen() calls for long PHP strings are almost as fast as a single call. In this PHP String Length example, we use the strlen() function to get the length of an ASCII string (without Unicode characters). Click Execute to run the PHP String Length example online and see the result.
Getting the Length of a String in PHP Execute
<?php
echo strlen("PHP String Length Example");
?>
Updated: Viewed: 7696 times

What is PHP?

PHP is an open-source scripting language used for web development that can be embedded in HTML code. PHP is recognized for its flexibility and sizable developer community. PHP can run on many operating systems, including Windows, Linux, and macOS, and supports multiple databases such as MySQL, PostgreSQL, and SQLite.

What is a string in PHP?

A PHP string consists of character sequences, where each character is represented by one byte. Since a byte can only introduce 256 characters, the Unicode strings cannot be supported natively in PHP. When creating a string in PHP, you can use single quotes ('...'), double-quoted strings ("..."), and Heredoc syntax (<<<). PHP provides many built-in functions for replacing, splitting, concatenating, interpolating, and comparing strings. You can also convert string to lowercase, convert string to int, convert string to array, and find the length of a string.

How to find the length of a PHP string?

To find the length of a PHP string, you can use the built-in strlen() function. The following is the syntax of the strlen() function:

PHP strlen() Syntax
strlen($string)

Where:
  • $string (required): the string whose length you want to return.
PHP strlen() Example
<?php
echo strlen("PHP String Length Example");
?>

#output: 25

How to get the number of visible characters in a PHP string?

If your string contains Unicode characters, then the strlen() function will return a string longer than the number of visible characters because the strlen() function returns the number of bytes, not characters, in the string. You can use the mb_strlen() function to get the number of visible characters. The following is the syntax of the mb_strlen() function:

PHP mb_strlen() Syntax
mb_strlen($string, 'utf8')

Where:
  • $string (required): the string whose length you want to return;
  • utf8 (optional): the parameter is a character encoding. If it is omitted or null, the internal character encoding value will be used.
PHP mb_strlen() Example
<?php
echo strlen("✅PHP String Length Example");
echo ' ';
echo mb_strlen("✅PHP String Length Example");
?>

#output: 28
#output: 26

PHP String Length Examples

The following are examples of getting the length of a string in PHP:

Getting the length of a string

Below is an example of getting the length of a string in PHP using the strlen() function:

Getting the Length of a String using strlen()
<?php
echo strlen("PHP String");
?>

#output: 10

Getting the length of a multibyte string

Below is an example of getting the length of a multibyte string in PHP:

Getting the Length of a Multibyte String
<?php
echo strlen("你好,世界");

echo mb_strlen("你好,世界");
?>

#output: 15
#output: 5

See also