Patterns Programs In PHP

To day we will print different logical Patterns in php

Lets start our first patter start from basic triangles, Perfect tutorial for be-gainer who want to improve there logical skill, and this tutorial also help you in interview

1) Patter of multiple triangles
2) Pyramid Pattern
3) Square border pattern
4) Square pattern
5) Triangle 1
6) Triangle 2
7) Triangle 3
  1. Patter of multiple triangles
    *                  ********************                  *
    **                *********    *********                **
    ***              ********        ********              ***
    ****            *******            *******            ****
    *****          ******                ******          *****
    ******        *****                    *****        ******
    *******      ****                        ****      *******
    ********    ***                            ***    ********
    *********  **                                **  *********
    ***********                                    ***********

Code:

<?php
$n=10;
for($i=0;$i<$n;$i++)
{
        for($j=0;$j<=$i;$j++)
        {
            echo "*";            
        }
        for($k=$n-1;$k>$i;$k--)
        {
            echo "  "; // Printing Blank space
        }
        for($l=$n;$l>$i;$l--)
        {
            echo "*";            
        }
        for($m=0;$m<$i;$m++)
        {
          echo "  ";  // Printing Blank space
        }
        for($m=0;$m<$i;$m++)
        {
          echo "  ";  // Printing Blank space
        }
        for($p=$n;$p>$i;$p--)
        {
            echo "*";   
        }
        for($q=$n-1;$q>$i;$q--)
        {
            echo "  ";  // Printing Blank space
        }
        for($r=0;$r<=$i;$r++)
        {
            echo "*";   
        }
        echo "</br>";
   
}

?>

2) Pyramid Pattern

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

Code :

<?php

$n=8;
for($i=0;$i<$n;$i++)
{   
    for($k=$n-1;$k>$i;$k--)
    {
        echo "  "; // Printing Blank space
    }
    for($j=0;$j<=$i;$j++)
    {
        echo "*";   
    }
    for($j=0;$j<$i;$j++)
    {
        echo "*";            
    }
   
    echo "</br>";  
}
for($i=0;$i<$n;$i++)
{   
    for($k=0;$k<=$i;$k++)
    {
        echo "  "; // Printing Blank space
    }
    for($j=$n-1;$j>$i;$j--)
    {
        echo "*";  // Printing *         
    }
    for($l=$n-2;$l>$i;$l--)
    {
        echo "*";    // Printing *
    }
   
    echo "</br>";  
}

?>

3) Square border pattern
**********
*                *
*                *
*                *
*                *
*                *
*                *
*                *
*                *
**********

Code:

<?php
$n=10;
for($i=0;$i<$n;$i++)
{ 
    for($k=0;$k<$n;$k++)
    {
       
        if($k==0 || $i==0 || $i==$n-1 ||$k==$n-1 )
        {
            echo "*";
        }
        else
        {
            echo "  ";
        }
    }
   
    echo "</br>";
}

?>

4) Square pattern

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

Code:

<?php
$n=5;
for($i=0;$i<$n;$i++)
{   
    for($j=0;$j<$n;$j++)
    {
        echo "*";    // Printing Blank space
    }
   
    echo "</br>";  
}

?>

5) Triangle 1

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

Code:

<?php
$n=5;
for($i=0;$i<$n;$i++)
{   
    for($k=0;$k<$i;$k++)
    {
        echo "  "; // Printing Blank space
    }
    for($j=$n;$j>$i;$j--)
    {
        echo "*";   
    }
    
    echo "</br>";  
}

?>

6) Triangle 2

Code:

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

Code:

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

7) Triangle 3

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

Code:

<?php
$n=5;
for($i=0;$i<$n;$i++)
{   
    for($k=$n-1;$k>$i;$k--)
    {
        echo "  "; // Printing Blank space
    }
    for($j=0;$j<=$i;$j++)
    {
        echo "*";   
    }
    echo "</br>";  
}
?>

8) Triangle 4

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

Code:

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

echo "Using While Loop ##################%%%%%%%%%%%%%%%%##################</br>";
$i=0;
 while($n>0)
 {
    for($j=0;$j<=$i;$j++)
    {
        echo "*";            
    }
    echo "</br>";
    $n--;
    $i++;
 }
?>

Hope you guys enjoy it let me know in comment section if i miss something

Add a Comment

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