This tutorial demonstrates a simple PHP implementation for printing design patterns. Pattern programs help beginners understand how loops work and make learning easier by practicing using various examples.



Generally, the pattern program is not helpful in the actual programming scenario, but you can learn how to use nested loops by practicing this type of program.

Print Simple Pyramid Pattern

Example:

PHP code to display left-aligned pyramid pattern using stars:

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

Program Output:

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

Print Reverse Pyramid Pattern

Example:

PHP code to display left-aligned and reverse pyramid patterns using stars.

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

Program Output:

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

Print Triangle Pyramid Pattern

Example:

PHP code to display triangle pyramid pattern program using stars:

<?php
//Star Pyramid Size
$size = 5;
for($i=1;$i<=$size;$i++){
    for($j=1;$j<=$size-$i;$j++){
        echo "&nbsp;&nbsp;";
    }
    for($k=1;$k<=$i;$k++){
                echo "*&nbsp;&nbsp;";
    }
echo "<br />";
}
?>

Program Output:

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

Print Square Pyramid Pattern

Example:

PHP code to display square pattern program using stars:

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

Program Output:

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

Print Simple Number Pattern

Example:

PHP code to display left-aligned pyramid pattern using numbers:

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

Program Output:

1 
2 2 
3 3 3 
4 4 4 4 
5 5 5 5 5

Print Simple Number Pattern (Continuous)

Example:

PHP code to display left-aligned pyramid pattern using numbers in series:

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

Program Output:

1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15

Print Character Pattern

Example:

PHP code to display left-aligned pyramid pattern using letter:

<?php
$n = 65; /*Initializing ASCII value of (A) */
for ($i = 0; $i < 5; $i++)
{
    for ($j = 0; $j <= $i; $j++ )
    {
        echo chr($n)." ";
    }
    $n = $n + 1;
    echo "<br/>";
}
?>

Program Output:

A 
B B 
C C C 
D D D D 
E E E E E


Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram