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.

echo "&nbsp;&nbsp;";
$d = 1;
    echo "*&nbsp;&nbsp";
echo "&nbsp;&nbsp;";


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.



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

Leave a Reply

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