Monday, 10 March 2014

String length function in php

strlen
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
Screenshot1


<?php



$string = 'Alex';



$string_length = strlen('How many Characters does this have?');



echo $string_length;



?>

Output (Screenshot 2):-

strlen function php
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):-

string length function
Screenshot3


<?php



$string = 'Alex';



$string_length = strlen('$string');



echo $string_length;



?>


output (Screenshot 4):-

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 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):-

strlen function loop
Screenshot5


<?php



$string = 'Alex';

$string_length = strlen($string);



for ($x=1;$x<=$string_length;$x++) {

echo $x.'<br>';

}

?>

output (Screenshot 6):-

string functions php
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..

About Me

Welcome to Extra Tutorials! My name is Mohammed and I am the 22 year writer, website developer, and photographer behind the blog. Thanks for visiting! Tutorials Jackpot… In addition to Developer, I love to develop websites and I love to write. Starting a php Blog was inevitable for me. What began as a simple way to share all of my Tutorials with friends and family has developed into my Part time job.

Mohammed Padela

WHAT IS PHP PROGRAMMING

WHAT IS PHP PROGRAMMING
WHAT IS PHP PROGRAMMING

Follow Us

Popular Posts

Designed ByBlogger Templates