Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

create star triangle with php

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading