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.
//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.
//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.
//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.
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.
<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.
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.