Creating a pyramid shape: PHP

To create a pyramid we need 3 repetitions for example as follows:


//PYRAMID TOP
for ($i = 0; $i < 5; $i++) {
 for ($j = 5; $j > $i; $j–) {
 echo '&nbsp';
 }
 for ($k = 0; $k <= $i; $k++) {
 echo '*';
 }
 echo '
';
 }


//PYRAMID BOTOM
 echo “

“;

 for ($i = 1; $i <= 5; $i++) {
    for ($j = 6; $j > $i; $j–) {
        echo '*';
    }
     for ($k = 2; $k <= $i; $k++) {
     echo '';
     }
 echo '
';
 }
?>


for ($ i); // repetition i to make the number of rows repetition of 0 to no more than 5, i ++ grow 0-4, echo '
', // to act line 


for ($ j); // repetitions to create a column j j 5 worth no more than i (0), 5-0 j– reduced, echo '& nbsp' // to display a blank character 

for ($ k); // repetitions k to fill column 0 as i no more and grow, echo '*'; // to produce a character * 


The result:

Creating a pyramid shape: PHP






If you have any questions please comment. 
Do not forget to like and share. 
learn Programming in fajaryusuf.com . 🙂

Spread the love

Fajar (Ucup)

The owner and author of FajarYusuf.com and also someone who likes to share interesting information with others.

Leave a Reply

Your email address will not be published. Required fields are marked *