Updated June 30, 2023
Introduction to PHP strlen()
A large set of built-in functions make it easier for the coder or the developer to deal with string-related operations in PHP. The strlen() is one of the popular functions we can say of the PHP string to get the length of the string. As its name has been formed by combining two words, str, and len, str refers to the word string, and the length is the length itself. This function will return the length of the given string. This can be used to get the length of the string of any substring whenever required in the program.
Syntax:
strlen(String)
Parameter:
- strlen: It is the function itself.
- String: This is the only parameter that takes as a string.
Return Type:
This returns the length of the string, and it will always be an Integer. There is nothing much with the syntax of this function.
How strlen() Function work in PHP?
Before moving ahead with the strlen, we must have a strong first upon which this function will be applied. It takes the string as a parameter and returns the length of that given string. We can use the characters, special characters, URL, numeric values, and special sequence characters as part of the given string.
The user of strlen ()
Code:
$string = "WELCOME TO INDIA"; // a string
echo "Lenght of the given String is: ";
echo strlen($string); // lenghtof the string
The above line of code given the length output as 16 as ‘WELCOME TO INDIA’ contains 16 characters, including the spaces.
Use of strlen() with the special sequence character as a string
We should be careful while passing a string containing special sequence characters like \n,\t,\r, etc. As we can see, we \n the 2 characters, but it will be counted as one while we will pass this as a parameter with the strlen() function.
Code:
$string = "\rHello"; // a string
$length = strlen($string); // lenghtof the string
echo "<br>Length: ".$length;
The above line of code gives the output as 6 rather than 7. If we replace \ with any other character, then we will see the length 7 as an output. This could be a bit tricky while coding in php using the string strlen() function.
Examples of PHP strlen()
Given below are the examples mentioned :
Example #1
Get the length of the given string.
Code:
<?php
$string = "WELCOME TO INDIA"; // a string
$Length = strlen($string); // lenghtof the string
echo "Length of the given String is: ".$Length;
?>
Output:
As we can see, there is not much in using this function due to the simplicity of this string function. We can consider this function as one of the easiest functions of the PHP string in nature, passing the string as a parameter to get the length.
Example #2
Get the length of the given string after removing all the spaces from that string.
Code:
<?php
$string = "WELCOME TO INDIA"; // a string
$lenght = strlen($string); // lenghtof the string
echo "Actual string: ".$string;
echo "\nLength of the given String including space: ".$lenght;
$stringAfterRemovedSpace = str_replace(" ","",$string);
echo "\nActual String with no spaces: ".$stringAfterRemovedSpace;
echo "\nLength of the given String excluding space: ".strlen($stringAfterRemovedSpace);
?>
Output:
Example #3
An example program demonstrates the use of strlen() function with the string containing the special character and the escape character.
Code:
<?php
$string = "\n WELCOME TO INDIA"; // a string
$Length = strlen($string); // lenghtof the string
echo "Length of the given String is: ".$Length;
?>
Output:
In the above program, when we count the character, it will come up to 19 in the manual, but it the program output, we can see that the length is showing 18. The length is showing one character lesser just because \n is being counted as one.
Example #4
Use of the strlen() function in a program to reverse the given string.
Code:
<?php
$string = "WELCOME TO INDIA"; // a string
$length = strlen($string); // lenghtof the string
echo "Actual String: ".$string;
echo "\nLength: ".$length;
echo "\nReverse String: ";
for ($i=($length-1) ; $i >= 0 ; $i--)
{
echo $string[$i];
}
?>
Output:
In the above example code, we can see that the reversing string in php comes up with various stages, which include the getting length of the string, too, because the length of the given string helps the developer of the coder to loop through the end to the first position of the string.
Example #5
Length of the string and length of the reverse string of the same.
Code:
<?php
$string = "\n WELCOME Home!!!"; // a string
$length = strlen($string); // lenghtof the string
echo "Actual String: ".$string;
echo "\nLength: ".$length;
echo "\nReverse String: ";
$reverseString = "";
for ($i=($length-1) ; $i >= 0 ; $i--)
{
$reverseString .= $string[$i];
}
echo $reverseString;
echo "\nLength of the Reverse String: ".strlen($reverseString);
?>
Output:
Conclusion
Built-in function strlen() can be used to get the length of the string. A developer should be aware enough of the functioning of this function. For example, if \n, \t, etc. contains in a string, then this 2-character special sequence can be considered as one. This function can be used to get the length of a string to make the further required operations. If we are writing our own custom string reverse code, then we can use this function to calculate the actual length of the string.
Recommended Articles
This is a guide to PHP strlen(). Here we discuss the introduction to PHP strlen(), how this function works, and programming examples. You may also have a look at the following articles to learn more –