String Formation Through Loops in PHP
Using loops, you can form strings.
Let's create a string filled with ten letters 'x'
as an example:
<?php
$str = '';
for ($i = 0; $i < 10; $i++) {
$str .= 'x';
}
echo $str;
?>
Now let's make a string '12345'
.
To do this, we will add the loop counter to our variable:
<?php
$str = '';
for ($i = 1; $i <= 5; $i++) {
$str = $str . $i;
}
echo $str; // will output '12345'
?>
Using a loop, form a string filled with 5
hyphens.
Using a loop, form the string '123456789'
.
Using a loop, form the string '987654321'
.
Using a loop, form the string '-1-2-3-4-5-6-7-8-9-'
.