I want to make a triangle like this with php , but i try in local still fail how to code like the example above
<?php
$star=10;
for($a=1; $a<=$star; $a++){
for($c=$star; $c>=$a; $c-=1){
echo "*";
}
echo "<br>";
}
?>
I have value 1225441. How to create output like this with looping php?
1000000
200000
20000
5000
400
40
1
>Solution :
You had the right idea with a for loop.
You just need to know how long your input is, and pad the output with the length of your input, gradually getting smaller with each iteration.
<?php
# Replace this with whatever is the source of your input.
$input = '1225441';
# get total number of characters in input
$length = strlen($input);
# foreach character in input
for ($i = 0; $i < $length; $i++) {
# echo the character as many times as $length - $i - 1.
echo $input[$i] . str_repeat(0, $length - $i - 1) . '<br>';
}
This creates the output of:
1000000
200000
20000
5000
400
40
1