PHP Program to find number of characters in the string using strlen() function


February 12, 2022, Learn eTutorial
1375

How to find the number of characters in the string using PHP?

In this program, we are finding the length of the string entered by the user. To do so we are using the built-in function strlen(). For this, we first have to read the string into the variable str and then we have to call the function strlen() with the variable str as the argument and assign it to the variable len. And at last print, the value of the variable len as the number of characters in the string entered.

Syntax of function strlen()


strlen(string)
 

ALGORITHM

Step 1: Accept the string from the user and assign it to the variable str

Step 2: To find the length of the string use the built-in function strlen() and the variable str as the argument and assign the result to the variable len

Step 3: Print the value of the variable len as the number of characters in the string entered

PHP Source Code

                                          <?php
$str = readline("Enter the string: ");
$len = strlen($str);
echo "Number of charachters in $str is : $len";
?>
                                      

OUTPUT

Enter the string: learnetutorials.com
Number of characters in learnetutorials.com is: 19