Introduction to PHP strlen()
A large set of build-in functions make easier for the coder or the developer to deal with the string related operations in the PHP. The strlen() is one of the popular function we can say of the PHP string to get the length of the string. As its name has been form by combining two words str and the len, str refer 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 return the length of the string and it will be always 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 string first upon which this function will be applied. It takes the string as a parameter and return the length of that given string. We can use the characters, special characters, URL, numeric values, and special sequence characters as the 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 the 16 characters including the spaces.
Use of strlen() with the special sequence character as an string
4.5 (6,077 ratings)
View Course
We should be careful enough while we are passing string which contains the special sequence characters like \n,\t,\r etc. As we can see we \n contains 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 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 nothing much in using this function due to the simplicity of this string function. We can consider this function as a one of the easiest functions of the PHP string in nature, passing the string as 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 19 in 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 about 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 does this function work along with programming examples. You may also have a look at the following articles to learn more –