number pyramid program in php | pyramid program in php

Write a Number pyramid program in PHP

It could be a brain booster exercise for those who recently started learning PHP and just started a loop. 

You have probably done a pyramid program in C if you have a programming background.

You would have to use a two-loop inside the loop, and you can create this kind of pyramidal structure of any symbol. 

Remember always there would be a loop which runs in decreasing order for creating spaces. 

In the following program, I added a query string so that you can generate any row pyramids only bypassing the query string in the URL.

<?php
$numberOfRow = $_GET['row']?? 10;
for($i=1;$i<=$numberOfRow;$i++){
	/*this is explanation of spaces*/
	//echo "<span style='color:#155fd6;'>".$numberOfRow-$i."</span>";
    for($j=1;$j<=$numberOfRow-$i;$j++){
        echo "&nbsp;&nbsp;";
    }
    for($k=1;$k<=$i;$k++){
		echo "▲&nbsp;&nbsp;";
    }
	echo "<br />";
}

?>

You can also read about how to check string in url.

Leave a Comment

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