php

Count the length of the string using the PHP strlen() function

The string data is an array of characters, and the length of the string data in PHP can be counted by using the strlen() function. It counts the length of a string that may contain alphabetic characters, numeric characters, special characters, and whitespaces. It counts the number of bytes for UTF8 encoded data. Different uses of the strlen() function in PHP have been shown in this tutorial.

Syntax:
The syntax of the strlen() function is given below.

strlen(string $string): int

It takes a string value as the mandatory argument, and this function will return the length of the string value.

Example-1: Counting the length of different data

Create the PHP file with the following script where the strlen() function has been applied for the string data, number data, and Boolean data.

<?php
   
    //Declare a string variable
    $text = "Learn PHP Programming";
    //Declare a number variable
    $number = 35000;
    //Declare a boolean value
    $bool = True;
     
    //Print the length of the string data
    echo "<br />The length of string data is ".strlen($text);
    //Print the length of the number data
    echo "<br />The length of number data is ".strlen($number);
    //Print the length of the Boolean data
    echo "<br />The length of boolean data is ".strlen($bool);
?>

Output:

The following output will appear after executing the above script. In the output, the total number of characters returned for the string data, the total number of digits returned for the number data, and 1 returned for the true value.

Example-2: Counting the of the string with escape characters

Create a PHP file with the following script that will count the length of the string with the escape characters. The ‘\t’ and ‘\n’ characters have been used in the string values of this script.

<?php
 
    //Define string with the'\t'
    $string1 = "PHP\tPython\tPerl";
    //Define string with the'\n'
    $string2 = "\nLinuxHint";
     
    //Print the length of the string variables
    echo "The first string value is: $string1"."<br />";
    echo "The length of the string is: ".strlen($string1)."<br />";
    echo "The second string value is: $string2"."<br />";
    echo "The length of the string is: ".strlen($string2);
?>

Output:

The following output will appear after executing the above script. The first string contains 15 characters where 2 are ‘\t’ characters, and 13 are alphabets. The second string contains 10 characters where 1 is ‘\n’ character and 9 are alphabets.

Example-3: Counting the length of a string with spaces

Create a PHP file with the following script that will count the length of a string that contains multiple spaces. Three string values have been declared in the script. The first string contains a string of 5 characters. The second string contains 5 spaces. The third-string contains 5 characters. The length of the second string and the length of the combined value of the three strings were printed later.

<?php
//Define a string variable with alphabets
$str1 = "Hello";
//Define a string with multiple spaces
$gap = '     ';
//Define another string variable with alphabets
$str2 = "World";

//Print the length of the variable that contains spaces
echo "The length of the variable containing spaces is: ".strlen($gap)."<br />";

//Concatenate three strings
$combine = $str1.$gap.$str2;
echo "<pre>The string value after concatenating three variables is: $combine</pre>";

//Print the length of the concatenated strings
echo "<br />The length of the conctenated string is: ".strlen($combine);
?>

Output:

The following output will appear after executing the above script. The output shows that the length of the first string is 5, and the length of the combined string is 15 (5+5+5).

Example-4: Counting the length of each element of the array

Create a PHP file with the following script that will count the total characters of each element of an array. The ‘foreach’ loop has been used to iterate each array element and count the length of each element.

<?php
//Deaclare an array
$languages = array('PHP','Bash','Python','C++','Java', 'Perl');
//Iterate the array values
foreach($languages as $value)
{
    //Print the length of array value
    echo "The length of <b>$value</b> is ".strlen($value)."<br />";
}

?>

Output:

The following output will appear after executing the above script.

Example-5: Validating form data

The strlen() function can be used for form validation. Create a PHP file with the following script to validate the username and the password fields of the login form by using the strlen() function. After validation, if the username and password are valid, the ‘Valid user’ message will be printed; otherwise, the ‘Invalid user’ message will be printed.

<html>
<body>

<form method="post" action="#">

<p>Username : <input type="text" name="username"></p>
<p>Password : <input type="password" name="password"></p>
<p><input type="submit" name="submit" value="Submit"></p>

</form>

<?php
if(isset($_POST['submit']))
{
    if(strlen($_POST['username']) < 5)
    {
        die ("Username must be minimum 5 characters long.");
    }
    if(strlen($_POST['password'])

</body>
</html>

Output:
The following output will appear after providing invalid username.

The following output will appear after providing a valid username and password.

The following output will appear after providing an invalid username and password.

Example-6: Return the number of bytes

Create a PHP file with the following script that will count the length of one string in characters and the length of another string in bytes. Both outputs are printed later.

<?php

   //Declare a string that will return characters as length
   $string1 = 'PHP 8.0';

   //Declare a string that will return bytes as length
   $string2 = 'ث';

   $len1 = strlen($string1);
   $len2 = strlen($string2);

   echo "The length of \$string1 is $len1<br />";
   echo "The length of \$string2 is $len2";

?>

Output:

The following output will appear after executing the above script.

Conclusion:

The ways to count the length of different string values using the strlen() function have been shown in this tutorial using multiple examples. After reading this tutorial, the PHP users will be able to use the strlen() function properly in their script.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.