PHP Program to separate the characters in the string


March 22, 2022, Learn eTutorial
1355

How to separate the characters of the string?

In this program, we are going to print each and every character in a string separately. For example, if the entered string is "Hello" then the output will be H e l l o

How to separate the characters of the string using PHP?

In this program, we are using the static string and it is assigned to the variable string. After that, we are reassigning it to the variable str after that we assign the length of the variable string into the variable size and then we assign the value 0 into the variable i and perform the printing until the condition 'i < size' becomes false and we increment the value of the variable i in every iteration and in the block of the loop we print the value of str[i].

ALGORITHM

Step 1: Assign the string into the variable string

Step 2: Assign the string in the variable string to the variable str

Step 3: Assign the length of the variable string into the variable size

Step 4: Assign the value 0 into the variable i and perform the sub-step until the condition 'i < size' becomes false and increment the value of the variable i in every iteration

        (i) Print the value of str[i]

 

PHP Source Code

                                          <?php
$string = "learnetutorials";
$str = $string;
$size = strlen($str);
echo "The entered string is: $string \n";
echo "Individual characters from given string: \n";
for ($i = 0; $i < $size; $i++) {
    echo "$str[$i] \n";
}
?>
                                      

OUTPUT

The entered string is: learnetutorials
Individual characters from given string:
l
e
a
r
n
e
t
u
t
o
r
i
a
l
s