How to print a star pattern without using loop in the php programming language
ADVERTISEMENTS
How to print a star pattern without using loop in the php programming language. Here, you will learn how to print the star pattern without using the loop.
Take an example to print this star pattern through a php program:
<?php
// How to print a star pattern without
// using loop in the PHP programming language
// It's the number of rows
$x = 10;
// This will print the pattern
starPattern($x);
// It's the recursive function
// to print the star pattern
function starPattern($n, $i = 1, $r = 1) {
if ((int)sqrt(pow(($i - (2 * $n - 1) * ($r - 1) - $n) , 2)) < $r)
echo "*";
else
echo " ";
if (($i - (2 * $n - 1) * ($r - 1)) % (2 * $n - 1) == 0) {
echo "\n";
$r++;
}
if ($i++ < $n * (2 * $n - 1)) starPattern($n, $i, $r);
}
?>
Output
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************