String function in php programming language. String length function why is so useful and how we can use it?
Code for String length function (Screenshot1):-
Screenshot1 |
<?php $string = 'Alex'; $string_length = strlen('How many Characters does this have?'); echo $string_length; ?>
Output (Screenshot 2):-
Screenshot2 |
containing string called $string_length. Now strlen() is the string function from which we can count length of string characters in string strlen() function will contain one parameter such as string which we want to count the character as you see example we are counting string length how many characters does this have it contain 35 characters in string.
Code for string length function (Screenshot 3):-
Screenshot3 |
<?php $string = 'Alex'; $string_length = strlen('$string'); echo $string_length; ?>
output (Screenshot 4):-
Screenshot4 |
In this example we had two variables we stored $string alex in variable which contain 4 characters then in strlen function we called parameters variable where we had stored string alex echo out $string_length.So, in this way we can count string length and output string length
Why we want to do this ??
There are few different reasons why we choose this strlen function the first main important reason is for count amount of characters in string. we may want to loop through each character using loops in php. we start from first character and where we will stop how we will know where is end of the string the answer is strlen function which will count length and we can use it in loops to each character in string
Code for for loop to each character of string (Screenshot 5):-
Screenshot5 |
<?php $string = 'Alex'; $string_length = strlen($string); for ($x=1;$x<=$string_length;$x++) { echo $x.'<br>'; } ?>
output (Screenshot 6):-
Screenshot6 |
In this example i am counting every and each character of the string by with for loop in php. This example is every useful when user will input character and we have to play with it.
No comments:
Post a Comment
Thanks For Comment Will get you Soon..