Hello Reader! All of us must have designed a code to print pyramid or some other pattern of asterisk using C, C++. Here is a simple program to print the different asterisk(stars) patterns in PHP. A pattern of stars is a way of arranging these in some logical manner or they may form a sequence.
/*Program to draw a pattern of asterisk using PHP */
<?php
echo "Pattern 1:<br>";
for ($i=0; $i <5 ; $i++)
{
for ($j=1; $j <5-$i ; $j++)
{
echo "   ";
}
for ($k=0; $k <=$i ; $k++)
{
echo "* ";
}
echo "<br>";
}
echo "Pattern 2:<br>";
for ($i=1; $i <=4 ; $i++)
{
for ($j=$i; $j < 4; $j++)
{
echo "  ";
}
for ($k=1; $k <($i*2) ; $k++)
{
echo "* ";
}
echo "<br>";
}
echo "Pattern 3:<br>";
for ($i=1; $i <=6 ; $i++)
{
for ($j=6; $j >=1 ; $j--)
{
if ($i==$j)
{
echo " * ";
}
else
{
echo " ";
}
}
for ($k=2; $k <=6 ; $k++)
{
if ($i==$k)
{
echo " * ";
}
else
{
echo " ";
}
}
echo "<br>";
}
echo "Pattern 4:<br>";
for ($i=1; $i <=6 ; $i++)
{
for ($j=6; $j >=1 ; $j--)
{
if ($i==$j)
{
echo " * ";
}
else
{
echo " ";
}
}
echo "<br>";
}
echo "Pattern 5:<br>";
for ($i=1; $i <=6 ; $i++)
{
for ($j=1; $j <=6 ; $j++)
{
if ($i==$j)
{
echo " * ";
}
else
{
echo " ";
}
}
echo "<br>";
}
?>
click here for output
0 Comment(s)