Monday, 21 October 2013

String length function in php


String length function in php
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):-
String length function in php
String length function (Screenshot1)
1
2
3
4
5
6
7
8
9
<?php
 
$string = 'Alex';
 
$string_length = strlen('How many Characters does this have?');
 
echo $string_length;
 
?>
Output (Screenshot 2):-
String length function in php
String length function (Screenshot2)

In this example we are finding how many characters are in the string so, i have created variable containing string called $string_length. Now strlen() is the string function from which we cancount 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):-
String length function in php
String length function (Screenshot3)
1
2
3
4
5
6
7
8
9
<?php
 
$string = 'Alex';
 
$string_length = strlen('$string');
 
echo $string_length;
 
?>
output (Screenshot 4):-
String length function in php
String length function (Screenshot4)
In this example we had two variables we stored $string alex in variable which contain 4 characters then in strlen function we called paramters 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 functionthe 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 functionwhich 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):-
String length function in php
String length function (Screenshot5)
1
2
3
4
5
6
7
8
9
<?php
 
$string = 'Alex';
$string_length = strlen($string);
 
for ($x=1;$x<=$string_length;$x++) {
echo $x.'<br>';
}
?>
output (Screenshot 6):-
String length function in php
String length function (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.

Read More Extra Tutorials

No comments:

Post a Comment