Print Star Pattern in PHP


Prev Tutorial Next Tutorial

Print Triangle of Stars in PHP

This is not new concept, you need to follow same concept like C programming to print star pattern or triangle of star.

Print Star Pattern in PHP

<?php

for ($row = 1; $row <= 5; $row++)
{
 echo "* </br>";
}

?>

Output

	
*
*
*
*
*

Print Triangle of Stars in PHP

<?php

for ($row = 1; $row <= 5; $row++)
{
 for ($col = 1; $col <= 5; $col++)
  {
   echo '* ';
  }
   echo "</br>";
}

?>

Output

	
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *

Print Star Pattern in PHP

<?php

for($i=0;$i<=5;$i++)
{
for($j=0;$j<=$i;$j++)
{
echo "* ";
}
echo "<br>";
}

?>

Output

	
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *

Print Star Pattern in PHP

<?php

for($i=0;$i<=5;$i++)
{
for($j=5-$i;$j>=1;$j--)
{
echo "* ";
}
echo "<br>";
}

?>

Output

	
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*

Print Triangle of Stars in PHP

<?php

for($a=5; $a>=1; $a--)
{
if($a%2 != 0)
{
for($b=5; $b>=$a; $b--)
{
echo "* ";
}
echo "<br>";
}
}
for($a=2; $a<=5; $a++)
{
 if($a%2 != 0)
{
 for($b=5; $b>=$a; $b--)
{
echo "* ";
}
echo "<br>";
}
}

?>

Output

	
*
* * *
* * * * *
* * *
* 

Print Star Pattern in PHP

<?php
for ($row = 1; $row <= 5; $row++)
{
    for ($col = 1; $col <= ($row > 3 ? 6 - $row : $row); $col++)
    {
        echo '* ';
    }

     echo "</br>";
}

?>

Output

	
*
* *
* * *
* *
*

Print Triangle of Stars in PHP

<?php

for($i=0;$i<=6;$i++)
{
for($k=6;$k>=$i;$k--)
{
echo "  ";
}
for($j=1;$j<=$i;$j++)
{
echo "*  ";
}
echo "<br>";
}
for($i=5;$i>=1;$i--)
{
for($k=6;$k>=$i;$k--)
{
echo "  ";
}
for($j=1;$j<=$i;$j++)
{
echo "*  ";
}
echo "<br>";
}

?>

Output

	
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*

Prev Tutorial Next Tutorial

Advertisements




Download Projects