Skip to main content

How to make pyramid with while loop in PHP

We need to print a Pyramid which prints stars so that a pryamid could be printed.

To do so, we need to print 1 star in the first line and 2 in the second line, 3 in the thrid line and so on. We also need to keep it in mind that the star will be printed in the center, so that it could form a triangular shape of pyramid.

<?php
$i=0;
while($i<=10){
echo "&nbsp;&nbsp;";
$c--;
 }
$d = 1;
while($d<=$i){
    echo "*&nbsp;&nbsp";
    $d++;
}
echo "&nbsp;&nbsp;";
}
?>


Explanation

There are three loops used in this program. First one is the outer one which is i, to create the horizontal row second one is $c which prints only spaces The third one is $d which prints stars according to demand.

The $c & $d are inside the $i loop, for every $i loop which prints horizontal line, the $c and $d will run just once to print space and * and then the <br/ >tag in the end will run once so that the cursor will go down to print another line.

Recent search terms:

  • how to print pyramid using do whilw loop in php
  • triangle in while loop in php
 

Amir

cPanel Level 1 Certified from cPanel University. Linux System Admin.

Leave a Reply

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