Stringlength PHP | How to use PHP strlen to get the string length?
Summary: In this article, you will learn Stringlength PHP and How to use PHP strlen to get the string length in PHP with Example? Let’s understand in details, I hope you will enjoy it!
Overview:
This tutorial covers a few things regarding strings in PHP:
1. The indexing of strings means that each character has an index number
2. How to find the length of a string or how many characters there are in a string?
3. How to ‘loop’ through every character of a string
This article is useful to find the length of a string and process each character of a string is something that will become very useful later on.
1. The indexing of strings
In every string, each character has an index or index number. therefore, in PHP, indexing starts from the number 0 (zero). That means that the letter T in the word “Tutorialscan” has an index of 0, the second letter “u” will have an index of 1, the third letter “t” will have an index of 2, the fourth letter “o” will have an index of 3, and so on… The given below image below shows an example of this.

Furthermore, you might, as an example, want to find out what the 5th character is in a string. You could do this by using its index number eg. $mystring[4]; would return the 5th character in the string.
program:
|
1 2 3 4 5 |
<?php $website = "Tutorialname"; echo $website[4]; } ?> |
2. How to find the length of a string
The length of a string means that how many characters are in the string. To get the length of a string, simply use the PHP strlen() function. The PHP strlen() function returns the length of the string on success (Stringlength PHP), and it will return 0 if the string is empty. furthermore, as you can see in the images the word "Tutorialscan" has 12 letters in it so its length is 12. means that Letters, numbers, spaces, & other characters are all counted in a string.
For example, finding the length of a string might be useful if you want to check that how much text was entered into a login form (eg. checking that a minimum of 10 characters was used for a password, or a maximum of 160 characters was used). let’s see the string function example.
program:
|
1 2 3 4 5 |
<?php $website = "Tutorialscan"; $websiteLength = strlen($website); echo $websiteLength; ?> |
3. Loop through each character of a string
Looping through the characters of a string. a for loop can be utilized to go through every single character in a string & process it. In the given below example, we do this to show each letter of a string on a new line. I think this example might not be all that useful, because there are several different ways that a for loop can be used to process characters in a string.
Program:
|
1 2 3 4 5 6 7 |
<?php $name = "Tutorialscan"; $nameLength = strlen($name); for($i=0;$i<$nameLength;$i++){ echo $name[$i] , '</br>'; } ?> |
u
t
o
r
i
a
l
s
c
a
n
Program:
|
1 2 3 4 5 6 |
<?php $myStr = "Hanuman 912!#h"; $myArray = str_split($myStr); foreach($myArray as $character){ echo $character . "<br>"; }?> |
a
n
u
m
a
n
9
1
2
!
#
h
Conclusion:
In this article, you have learned Stringlength PHP and How to use PHP strlen to get the string length in PHP with Example I hope you will enjoy it!. if have any query related program then contact me at info@tutorialscan.com I will try to resolve the problem. Stay healthy, stay safe! Happy coding! enjoy it!