PHP Star pattern programming series

Pattern – 1

                    

<?php
for($i=1;$i<=5;$i++)
{

for($j=1;$j<=$i;$j++)
{
echo ” *”;
}
echo “<br>”;
}

?>

Output

 

Pattern – 2

                    

<?php
$val=4;
for($i=1;$i<=5;$i++)
{

for($k=1;$k<=$val;$k++)
{
echo “_”;
}
$val–;
for($j=1;$j<=$i;$j++)
{
echo “*”;
}
echo “<br>”;
}
?>

Output

 

Pattern – 3

                    

<?php
for($i=1;$i<=5;$i++)
{
for($j=1;$j<=5;$j++)
{
echo ” *”;
}
echo “<br>”;
}
?>

Output

 

 

Pattern – 4

                    

<?php
for($i=5;$i>=1;$i–)
{
for($j=1;$j<=$i;$j++)
{
echo ” *”;
}
echo “<br>”;
}
?>

Output

 

Pattern – 5

                    

<?php
$val=6;
for($i=1;$i<=5;$i++)
{
for($j=2;$j<=$i;$j++) // print space
{
echo “_”;
}
for($k=1;$k<=$val-$i;$k++) //print star
{
echo “*”;
}
echo “<br>”;
}
?>

Output

 

Pattern – 6

                    

<?php
for($i=1;$i<=5;$i++)
{
if($i%2!=0) //odd
{
for($j=1;$j<=$i;$j++)
{
echo ” *”;
}
echo “<br>”;
}
}
for($k=3;$k>=1;$k–)
{
if($k%2!=0) //odd
{
for($l=1;$l<=$k;$l++)
{
echo ” *”;
}
echo “<br>”;
}
}
?>

Output

 

Pattern – 7

                    

<?php

for($i=1;$i<=3;$i++)
{
for($j=1;$j<=$i;$j++)
{
echo ” *”;
}
echo “<br>”;
}
for($k=2;$k>=1;$k–)
{
for($l=1;$l<=$k;$l++)
{
echo ” *”;
}
echo “<br>”;
}
?>

Output

 

Pattern – 8

                    

<?php

for($i=1;$i<=7;$i++)
{
for($j=1;$j<=$i;$j++)
{
echo ” *”;
}
echo “<br>”;
}
for($k=6;$k>=1;$k–)
{
for($l=1;$l<=$k;$l++)
{
echo ” *”;
}
echo “<br>”;
}
?>

Output

 

 

Pattern – 9

                    

<?php
function caller()
{
threestar();
fivestar();
sevenstar();
fivestar();
sevenstar();
fivestar();
}
function threestar()
{
for($i=1;$i<=2;$i++) // row loop
{
for($k=2;$k>=1;$k–) //space loop
{
echo “_”;
}
for($j=1;$j<=3;$j++) // star print loop
{
echo “*”;
}
echo “<br>”;
}
}
function fivestar()
{
for($i=1;$i<=1;$i++)
{
for($k=1;$k>=1;$k–)
{
echo “_”;
}
for($j=1;$j<=5;$j++)
{
echo “*”;
}
echo “<br>”;
}
}
function sevenstar()
{
for($i=1;$i<=3;$i++)
{
for($j=1;$j<=7;$j++)
{
echo “*”;
}
echo “<br>”;
}
}

echo “Program for displaying pattern of Coke Star<br><br>”;
caller();
?>

Output

 

Pattern – 10

                    

<?php
for($i=1;$i<=9;$i++)
{
for($k=9;$k>=1;$k–)
{
if($k%2==0)//space
{
if($k==8)
{
for($a=1;$a<=8;$a++)
{
echo “_”;
}

}elseif($k==6)
{
for($a=1;$a<=6;$a++)
{
echo “_”;
}
}
elseif($k==4)
{
for($a=1;$a<=4;$a++)
{
echo “_”;
}
}
elseif($k==2)
{
for($a=1;$a<=2;$a++)
{
echo “_”;
}
}

}
}

if($i%2!=0)//star
{
for($j=1;$j<=$i;$j++)
{
echo ” *”;
}
echo “<br>”;
}
}
?>

Output

3 Replies to “PHP Star pattern programming series”

Leave a Reply

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