PHP strlen() Function

What is PHP strlen() function?

If you want to know the length of a string, use strlen() function (Check example 1). Remember, the function counts the length in bytes not in characters and this function also treats a character as 1 byte. That means, it returns the number of bytes exists in the provided string. If there exists at least one multi-byte character in the string, the function won’t return the correct character number. This function also counts the length of whitespace, special characters (Check example 2).

Most of us uses UTF-8 character encoding system in our web application and It supports multi-byte character set. A UTF-8 character can consist 1 to 4 bytes. The UTF-8 character encoding system is ASCII compactable which is a single-byte character encoding system.

If the provided string consists any multi-byte character (Check example 3), the function returns a higher number as string length than the string’s original character number. To get character length of a multi-byte string use mb_length() instead.

Syntax:

strlen(string)

Return Values:

It returns the number of bytes in the provided string.

Examples:

Example 1: Finding length of a string-

<?php  // EXAMPLE 1
$string = "SchoolsOfWeb";
echo "The length of the string '$string' is: ".strlen($string);
?>

Output:

The length of the string ‘SchoolsOfWeb’ is: 12

Explanation:

The function tells us that the string is 12 character long. As each character of the string “SchoolsOfWeb” consists of 1 byte, the 12 is also the character number too.

Example 2: Finding length that consists special characters-

<?php  // EXAMPLE 2
$string = "Schools Of Web &>";
echo "The length of the string '$string' is: ".strlen($string);
?>

Output:

The length of the string ‘Schools Of Web &>’ is: 17

Explanation:

The function tells us that the string is 17 bytes long. As each character of the string consists of 1 byte, the 17 is also the character number too. Note, the string also consists of whitespaces and special characters and the function also counts those.

Example 3: Finding length that consists multi-byte characters-

<?php  // EXAMPLE 3
$string = "£©€";
echo "The length of the string '$string' is: ".strlen($string);
?>

Output:

The length of the string ‘£©€’ is: 7

Explanation:

The function tells us that the string is 7 bytes long. Though, apparently, there are 3 characters, but, these are multi-byte characters. The £ character is 3-byte and the remaining two characters are 2-byte long. As a result, the byte number and character number are nit same here.

Example 4: Finding length that consists escape sequence-

<pre>
<?php  // EXAMPLE 4
$string = "\t";
echo "The length of the string '$string' is: ".strlen($string);
?>
</pre>

Output:

The length of the string ‘             ‘ is: 1

Explanation:

Apparently, the string has 2 characters – “\” and “t”. But, the function considers it collectively one character and it is one byte long. So, it returns 1.

Example 5: Finding length of an empty string-

<?php  // EXAMPLE 5
$string = "";
echo "The length of the string '$string' is: ".strlen($string);
?>

Output:

The length of the string ” is: 0

Explanation:

As the empty string character, the function returns 0 as its length as expected.

Example 6: Finding length of a NULL variable-

<?php  // EXAMPLE 6
$string = NULL;
echo "The length of the NULL string is: ".strlen($string);
?>

Output:

The length of the NULL string is: 0

Explanation:

Though the NULL variable’s length is not equal to 0, the function returns 0 unexpectedly.

Practical Usages of strlen() Function:

  • To validate form inputs: Username could be 5-12 characters long. You can use this function to check whether provided username is within the range in the server side if you think the string consists only single byte characters.
  • To validate database inputs: If a field’s length is smaller than the value you attempt to enter, you’ll get an incomplete value when you retrieve that value later. So, before inserting a value into a table, you check its length by strlen() function if the string consists only single byte characters.
  • To know the storage size: If you want to know byte size of a string, use strlen() function if the string consists only single byte characters.

Notes on strlen() Function:

  • strlen() function considers length of each special sequence character as one not two. A special sequence character starts with a backslash(\) followed by another character, example – \n, \t, \r etc. Check example 4.
  • strlen() function doesn’t return expected value for NULL variable. It returns 0. Check example 6.

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP strlen() Function

strlen() is a one of the most used built-in PHP string function. You can get correct character number of a string if it only consists of 1-byte characters as this function counts number in byte. On the other hand, you’ll incorrect wrong character number if there is any multi-byte character in the string. In this case use mb_strlen() function. Use strlen() function carefully.

Reference:

https://www.php.net/manual/en/function.strlen.php

https://www.php.net/manual/en/regexp.reference.escape.php