How to make a numeric Polygon in PHP ?

We get the output of a numeric polygon using php ‘Loops’ with logic. Here we use eight Loops for print this diamond patterns in php.
– First and fifth are main and other are nested in these.
– First ‘Loop’ is used for number of lines in upper triangle and fifth Loop is used for number of lines in lower triangle.
– Second loop is used to print spaces, third loop is used for left side right angle triangle that will show counting according line number.
– Fourth loop is used for right side right angle triangle that will show reverse counting according to last line number.
– Sixth loop is used to print spaces and seventh is used to show counting in reverse order in lower left triangle (right angle triangle) and eighth is used to show counting in reverse order in right side triangle(right angle triangle).
– Here $n is used for number of lines in first loop and number of spaces in second loop.
syntax with output as below:

<?php
$m=1; $n=9; $z=9;
for($i=1; $i<=$n; $i++) {
	for($j=$i; $j<=$n-1; $j++) {
	   echo "&nbsp;&nbsp;&nbsp;";
	}
	for($k=1; $k<=$m; $k++)	{
		echo $k ."&nbsp";
	}
	for($c=$m; $c>1; $c--) {
		echo $c-1 ."&nbsp";
	}
	echo "<br>";
	$m++;
}
for($i=1; $i<=$n; $i++) {
	for($j=1; $j<=$i; $j++) {
	   echo "&nbsp;&nbsp;&nbsp;";
	}
	for($k=1; $k<$z; $k++) {
	    echo $k ."&nbsp";
	}
	for($c=$z-2; $c>=1; $c--){
	    echo $c ."&nbsp";
	}
	echo "<br>";
	$z--;
} ?>
how-to-print-a-numeric-polygon
4 Comments

Leave a Reply