Print alphabets with star pattern in PHP

by  
Share:
print-alphabets-star-pattern-programs-php

Learn PHP star pattern programs to print alphabets (A-Z).  The logic used here can be used to solve the same problems in any other programming language like C, C++, Java, etc.

If you are a pro try solving some of them without looking at the logic.

PHP program to print letter A with star pattern

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

Program Logic & Code
  • Take 11 rows and 10 columns, for the 6th row, will be the exact middle.
  • On the first and middle row, print star in all columns, use this condition ($row == 1 || $row == $row_middle)
  • Print star at the first and last column of each row, use this condition ( $column == 1 || $column == $column_length)
  • If the condition does not satisfy, we print spaces.

<?php
$row_length = 11;
$column_length = 10;
$row_middle = ceil($row_length / 2);

for ($row = 1; $row <= $row_length; $row++) {
    for ($column = 1; $column <= $column_length; $column++) {
        if ($row == 1 || $row == $row_middle) {
            echo "*&nbsp;";
        } else if ($column == 1 || $column == $column_length) {
            echo "*&nbsp;";
        } else {
            echo "&nbsp;&nbsp;&nbsp;";
        }
    }
    echo "<br>";
}
?>

PHP program to print letter B with star pattern

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

Program Logic & Code
  • Take 11 rows and 10 columns, for the 6th row, will be the exact middle.
  • On the first, middle and last row, print star in all columns, use this condition ($row == 1 || $row == $row_middle || $row == $row_length)
  • Print star at the second and last column of each row, use this condition ( $column == 2 || $column == $column_length)
  • If the condition does not satisfy, we print spaces.

<?php
$row_length = 11;
$column_length = 10;
$row_middle = ceil($row_length / 2);

for ($row = 1; $row <= $row_length; $row++) {
    for ($column = 1; $column <= $column_length; $column++) {
        if ($row == 1 || $row == $row_middle || $row == $row_length ) {
            echo "*&nbsp;";
        } else if ($column == 2 || $column == $column_length) {
            echo "*&nbsp;";
        } else {
            echo "&nbsp;&nbsp;&nbsp;";
        }
    }
    echo "<br>";
}
?>

PHP program to print letter C with star pattern

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

Program Logic & Code
  • Take 10 rows and 10 columns.
  • On the first and last row, print star in all columns, use this condition ($row == 1 || $row == $row_length)
  • Print star at the first column of each row, use this condition ($column == 1)
  • If the condition does not satisfy, we print spaces.

<?php
$row_length = 10;
$column_length = 10;

for ($row = 1; $row <= $row_length; $row++) {
    for ($column = 1; $column <= $column_length; $column++) {
        if ($row == 1 || $row == $row_length ) {
            echo "*&nbsp;";
        } else if ($column == 1) {
            echo "*&nbsp;";
        } else {
            echo "&nbsp;&nbsp;&nbsp;";
        }
    }
    echo "<br>";
}
?>

PHP program to print letter D with star pattern

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

Program Logic & Code
  • Take 10 rows and 10 columns.
  • On the first and last row, print star in all columns, use this condition ($row == 1 || $row == $row_length)
  • Print star at the second and last column of each row, use this condition ( $column == 2 || $column == $column_length)
  • If the condition does not satisfy, we print spaces.

<?php
$row_length = 10;
$column_length = 10;

for ($row = 1; $row <= $row_length; $row++) {
    for ($column = 1; $column <= $column_length; $column++) {
        if ($row == 1 || $row == $row_length ) {
            echo "*&nbsp;";
        } else if ($column == 2 || $column == $column_length) {
            echo "*&nbsp;";
        } else {
            echo "&nbsp;&nbsp;&nbsp;";
        }
    }
    echo "<br>";
}
?>

PHP program to print letter E with star pattern

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

Program Logic & Code
  • Take 11 rows and 10 columns, for the 6th row, will be the exact middle.
  • On the first, middle and last row, print star in all columns, use this condition ($row == 1 || $row == $row_middle || $row == $row_length)
  • Print star at the first column of each row, use this condition ($column == 1)
  • If the condition does not satisfy, we print spaces.

<?php
$row_length = 11;
$column_length = 10;
$row_middle = ceil($row_length / 2);

for ($row = 1; $row <= $row_length; $row++) {
    for ($column = 1; $column <= $column_length; $column++) {
        if ($row == 1 || $row == $row_middle || $row == $row_length ) {
            echo "*&nbsp;";
        } else if ($column == 1) {
            echo "*&nbsp;";
        } else {
            echo "&nbsp;&nbsp;&nbsp;";
        }
    }
    echo "<br>";
}
?>

PHP program to print letter F with star pattern

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

Program Logic & Code
  • Take 11 rows and 10 columns, for the 6th row, will be the exact middle.
  • On the first and middle row, print star in all columns, use this condition ($row == 1 || $row == $row_middle)
  • Print star at the first column of each row, use this condition ( $column == 1 )
  • If the condition does not satisfy, we print spaces.

<?php
$row_length = 11;
$column_length = 10;
$row_middle = ceil($row_length / 2);

for ($row = 1; $row <= $row_length; $row++) {
    for ($column = 1; $column <= $column_length; $column++) {
        if ($row == 1 || $row == $row_middle ) {
            echo "*&nbsp;";
        } else if ($column == 1) {
            echo "*&nbsp;";
        } else {
            echo "&nbsp;&nbsp;&nbsp;";
        }
    }
    echo "<br>";
}
?>

PHP program to print letter G with star pattern

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

Program Logic & Code
  • Take 11 rows and 10 columns, for the 6th row, will be the exact middle.
  • On the first and last row, print star in all columns, use this condition ($row == 1 || $row == $row_length)
  • Print star at the first column of each row, use this condition ( $column == 1)
  • Finally, from the middle till second-to-last column, print star in the last column only, use this condition ( $row >= $row_middle && $column == $column_length)
  • If the condition does not satisfy, we print spaces.

<?php
$row_length = 11;
$column_length = 10;
$row_middle = ceil($row_length / 2);

for ($row = 1; $row <= $row_length; $row++) {
    for ($column = 1; $column <= $column_length; $column++) {
        if ($row == 1 || $row == $row_length) {
            echo "*&nbsp;";
        } else if ($column == 1) {
            echo "*&nbsp;";
        } else if ($column == $column_length && $row >= $row_middle) {
            echo "*&nbsp;";
        } else {
            echo "&nbsp;&nbsp;&nbsp;";
        }
    }
    echo "<br>";
}
?>

PHP program to print letter H with star pattern

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

Program Logic & Code
  • Take 11 rows and 10 columns, for the 6th row, will be the exact middle.
  • On the middle row, print star in all columns, use this condition ($row == $row_middle)
  • Print star at the first and last column of each row, use this condition ( $column == 1 || $column == $column_length)
  • If the condition does not satisfy, we print spaces.

<?php
$row_length = 11;
$column_length = 10;
$row_middle = ceil($row_length / 2);

for ($row = 1; $row <= $row_length; $row++) {
    for ($column = 1; $column <= $column_length; $column++) {
        if ($row == $row_middle ) {
            echo "*&nbsp;";
        } else if ($column == 1 || $column == $column_length ) {
            echo "*&nbsp;";
        } else {
            echo "&nbsp;&nbsp;&nbsp;";
        }
    }
    echo "<br>";
}
?>

PHP program to print letter I with star pattern

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

Program Logic & Code
  • Take 10 rows and 11 columns, for the 6th column, will be the exact middle.
  • On the first and last row, print star in all columns, use this condition ($row == 1 || $row == $row_length)
  • Print star at the middle column of each row, use this condition ( $column == $column_middle)
  • If the condition does not satisfy, we print spaces.

<?php
$row_length = 10;
$column_length = 11;
$column_middle = ceil($column_length / 2);

for ($row = 1; $row <= $row_length; $row++) {
    for ($column = 1; $column <= $column_length; $column++) {
        if ($row == 1 || $row == $row_length ) {
            echo "*&nbsp;";
        } else if ($column == $column_middle) {
            echo "*&nbsp;";
        } else {
            echo "&nbsp;&nbsp;&nbsp;";
        }
    }
    echo "<br>";
}
?>

PHP program to print letter J with star pattern

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

Program Logic & Code
  • Take 10 rows and 11 columns, for the 6th column, will be the exact middle.
  • On the first row, print star in all columns, use this condition ($row == 1)
  • Print star at the middle column of each row, use this condition ( $column == $column_middle)
  • Finally, on the last row, print star from first till middle column, use this condition ($row == $row_length && $column < $column_middle)
  • If the condition does not satisfy, we print spaces.

<?php
$row_length = 10;
$column_length = 11;
$column_middle = ceil($column_length / 2);

for ($row = 1; $row <= $row_length; $row++) {
    for ($column = 1; $column <= $column_length; $column++) {
        if ($row == 1 ) {
            echo "*&nbsp;";
        } else if ($column == $column_middle) {
            echo "*&nbsp;";
        } else if ($column < $column_middle && $row == $row_length) {
            echo "*&nbsp;";
        } else {
            echo "&nbsp;&nbsp;&nbsp;";
        }
    }
    echo "<br>";
}
?>

PHP Program to print letter K with star pattern

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

Program Logic & Code
  • The program is divided into two parts i.e. upper and lower sections.
  • Take 10 rows and 5 columns. 5X5 grid twice.
  • In the upper section, print star on first column, then spaces and then star. Observe as row increases, spaces between stars decrease. use this condition ( $column <= $row_length – $row)
  • In the lower section, print star on the first column, then spaces and then star. Observe as row increases, spaces between stars also increase. use this condition ( $column == $row)

<?php
$row_length = 5;
$column_length = 5;

// upper section
for ($row = 1; $row <= $row_length; $row++) {
    echo "*&nbsp;";
    for ($column = 1; $column <= $column_length; $column++) {

        if ($column <= ($row_length - $row)) {
            echo "&nbsp;&nbsp;&nbsp;";
        } else {
            echo "*&nbsp;";
            break;
        }
    }
    echo "<br>";
}
// lower section
for ($row = 1; $row <= $row_length; $row++) {
    echo "*&nbsp;";
    for ($column = 1; $column <= $column_length; $column++) {

        if ($column == $row) {
            echo "*&nbsp;";
        } else {
            echo "&nbsp;&nbsp;&nbsp;";
        }
    }

    echo "<br>";
}
?>

PHP Program to print letter L with star pattern

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

Program Logic & Code
  • Take 10 rows and 10 columns.
  • On the last row, print star in all columns, use this condition ($row == $row_length)
  • Print star at the first column of each row, use this condition ( $column == 1)
  • If the condition does not satisfy, we print spaces.

<?php
$row_length = 10;
$column_length = 10;

for ($row = 1; $row <= $row_length; $row++) {
    for ($column = 1; $column <= $column_length; $column++) {
        if ($row == $row_length ) {
            echo "*&nbsp;";
        } else if ($column == 1) {
            echo "*&nbsp;";
        } else {
            echo "&nbsp;&nbsp;&nbsp;";
        }
    }
    echo "<br>";
}
?>

PHP program to print letter M with star pattern

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

Program Logic & Code
  • Take 10 rows and 11 columns, for the 6th column, will be the exact middle.
  • On the first row, print star in all columns, use this condition ($row == 1)
  • Print star at the first and last column of each row, use this condition ( $column == 1 || $column == $column_length)
  • From the first till middle row, print star in the middle column, use this condition ( $row <= $row_length/2 && $column == $column_middle)
  • If the condition does not satisfy, we print spaces.

<?php
$row_length = 10;
$column_length = 11;
$column_middle = ceil($column_length / 2);

for ($row = 1; $row <= $row_length; $row++) {
    for ($column = 1; $column <= $column_length; $column++) {
        if ($row == 1) {
            echo "*&nbsp;";
        } else if ($column == 1 || $column == $column_length) {
            echo "*&nbsp;";
        } else if ($column == $column_middle && $row <= ($row_length/2)) {
            echo "*&nbsp;";
        } else {
            echo "&nbsp;&nbsp;&nbsp;";
        }
    }
    echo "<br>";
}
?>

PHP program to print letter N with star pattern

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

Program Logic & Code
  • Take 10 rows and 10 columns.
  • For first and last column, print star in all rows, use this condition ($column == 1 || $column == $column_length)
  • From second row till second-to-last row, print star at current row equals to current column (ascending order) only, use this condition ( $column == $row)
  • If the condition does not satisfy, we print spaces.

<?php
$row_length = 10;
$column_length = 10;

for ($row = 1; $row <= $row_length; $row++) {
    for ($column = 1; $column <= $column_length; $column++) {
        if ($column == 1 || $column == $column_length) {
            echo "*&nbsp;";
        } else if ($column == $row) {
            echo "*&nbsp;";
        } else {
            echo "&nbsp;&nbsp;&nbsp;";
        }
    }
    echo "<br>";
}
?>

PHP program to print letter O with star pattern

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

Program Logic & Code
  • The code logic is the same as the program to print number zero with star pattern.
  • Take 10 rows and 10 columns.
  • On the first and last row, print star in all columns, use this condition ($row == 1 || $row == $row_length)
  • Print star at the first and last column of each row, use this condition ( $column == 1 || $column == $column_length)
  • If the condition does not satisfy, we print spaces.

<?php
$row_length = 10;
$column_length = 10;

for ($row = 1; $row <= $row_length; $row++) {
    for ($column = 1; $column <= $column_length; $column++) {
        if ($row == 1 || $row == $row_length) {
            echo "*&nbsp;";
        } else if ($column == 1 || $column == $column_length) {
            echo "*&nbsp;";
        } else {
            echo "&nbsp;&nbsp;&nbsp;";
        }
    }
    echo "<br>";
}
?>

PHP program to print letter P with star pattern

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

Program Logic & Code
  • Take 11 rows and 10 columns, for the 6th row, will be the exact middle.
  • On the first and middle row, print star in all columns, use this condition ($row == 1 || $row == $row_middle)
  • Print star at the first column of each row, use this condition ($column == 1)
  • From second till middle row, print star in the last column only, use this condition ($column == $column_length && $row < $row_middle)
  • If the condition does not satisfy, we print spaces.

<?php
$row_length = 11;
$column_length = 10;
$row_middle = ceil($row_length / 2);

for ($row = 1; $row <= $row_length; $row++) {
    for ($column = 1; $column <= $column_length; $column++) {
        if ($row == 1 || $row == $row_middle) {
            echo "*&nbsp;";
        } else if ($column == 1) {
            echo "*&nbsp;";
        } else if ($column == $column_length && $row < $row_middle) {
            echo "*&nbsp;";
        } else {
            echo "&nbsp;&nbsp;&nbsp;";
        }
    }
    echo "<br>";
}
?>

PHP program to print letter Q with star pattern

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

Program Logic & Code
  • Take 10 rows and 10 columns.
  • On the first and last row, print star in all columns, use this condition ($row == 1 || $row == $row_length)
  • Print star at the first and last column of each row, use this condition ( $column == 1 || $column == $column_length)
  • Finally for the inner part, print star from mid row till second-to-last row where current column is equals to current row (ascending order), use this condition ($column == $row && $row >= $row_middle)
  • If the condition does not satisfy, we print spaces.

<?php
$row_length = 10;
$column_length = 10;
$row_middle = ceil($row_length / 2);

for ($row = 1; $row <= $row_length; $row++) {
    for ($column = 1; $column <= $column_length; $column++) {
        if ($row == 1 || $row == $row_length) {
            echo "*&nbsp;";
        } else if ($column == 1 || $column == $column_length) {
            echo "* ";
        } else if ($row >= $row_middle && $column == $row) {
           echo "* ";
        } else {
            echo "&nbsp;&nbsp;&nbsp;";
        }
    }
    echo "<br>";
}
?>

PHP program to print letter R with star pattern

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

Program Logic & Code

<?php
$row_length = 5;
$column_length = 5;
// upper section
for ($row = 1; $row <= $row_length; $row++) {
    for ($column = 1; $column <= $column_length; $column++) {
        if ($row == 1 || $row == $row_length) {
            echo "*&nbsp;";
        } else if ($column == 1 || $column == $column_length) {
            echo "*&nbsp;";
        } else {
            echo "&nbsp;&nbsp;&nbsp;";
        }
    }
    echo "<br>";
}
// lower section
for ($row = 1; $row <= $row_length; $row++) {
    echo "*&nbsp;";
    for ($column = 1; $column <= $column_length; $column++) {
        if ($column == $row ) {
            echo "*&nbsp;";
        } else {
            echo "&nbsp;&nbsp;&nbsp;";
        }
    }

    echo "<br>";
}
?>

PHP program to print letter S with star pattern

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

Program Logic & Code
  • The code logic is the same as the program to print number five with star pattern.
  • Take 11 rows and 10 columns, for the 6th row, will be the exact middle.
  • On the first, middle and last row, print star in all columns, use this condition ($row == 1 || $row == $row_middle || $row ==  $row_length)
  • From second till middle row, print star in the first column, use this condition ( $column == 1 && $row < $row_middle )
  • Between middle and last row, print star in the last column, use this condition ( $column == $column_length && $row > $row_middle )
  • If the condition does not satisfy, we print spaces.

<?php
$row_length = 11;
$column_length = 10;
$row_middle = ceil($row_length / 2);

for ($row = 1; $row <= $row_length; $row++) {
    for ($column = 1; $column <= $column_length; $column++) {
        if ($row == 1 || $row == $row_middle || $row == $row_length ) {
            echo "*&nbsp;";
        } else if ($column == 1 && $row < $row_middle) {
            echo "*&nbsp;";
        } else if ($column == $column_length && $row > $row_middle) {
            echo "*&nbsp;";
        } else {
            echo "&nbsp;&nbsp;&nbsp;";
        }
    }
    echo "<br>";
}
?>

PHP program to print letter T with star pattern

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

Program Logic & Code
  • Take 10 rows and 11 columns, for the 6th column, will be the exact middle.
  • On the first row, print star in all columns, use this condition ($row == 1)
  • Print star at the middle column of each row, use this condition ($column == $column_middle)
  • If the condition does not satisfy, we print spaces.

<?php
$row_length = 10;
$column_length = 11;
$column_middle = ceil($column_length / 2);

for ($row = 1; $row <= $row_length; $row++) {
    for ($column = 1; $column <= $column_length; $column++) {
        if ($row == 1) {
            echo "*&nbsp;";
        } else if ($column == $column_middle) {
            echo "*&nbsp;";
        } else {
            echo "&nbsp;&nbsp;&nbsp;";
        }
    }
    echo "<br>";
}
?>

PHP program to print letter U with star pattern

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

Program Logic & Code
  • Take 10 rows and 10 columns.
  • From first row till last row, print star in the first and last column, use this condition ( $column == 1 || $column == $column_length)
  • Print star at the last row of each column, use this condition ($row == $row_length)
  • If the condition does not satisfy, we print spaces.

<?php
$row_length = 10;
$column_length = 10;

for ($row = 1; $row <= $row_length; $row++) {
    for ($column = 1; $column <= $column_length; $column++) {
        if ( $row == $row_length) {
            echo "*&nbsp;";
        } else if ($column == 1 || $column == $column_length) {
            echo "*&nbsp;";
        } else {
            echo "&nbsp;&nbsp;&nbsp;";
        }
    }
    echo "<br>";
}
?>

PHP program to print letter V with star pattern

*                 * 
  *             *    
    *         *       
      *     *          
        * *             

Program Logic & Code
  • Break the program into two sections, left & right.
  • Take 10 rows and 5 columns (loop twice, side by side).
  • Start an outer loop and iterate it 5 times.
  • In the left section, its a combination of space & star. As row increases spaces increases, after that print star when column equals to the current row. use this condition ($column == $row)
  • In the right section, its a combination of space & star as well. As row increases spaces decrease, after that print star when the column equals to column length minus current row. use this condition ($column == ($column_length-$row)+1)
  • If the condition does not satisfy, we print spaces.

<?php
$row_length = 5;
$column_length = 5;
for ($row = 1; $row <= $row_length; $row++) {

    for ($column = 1; $column <= $column_length; $column++) {

        if ($column == $row) {
            echo "*&nbsp;";
        } else {
            echo "&nbsp;&nbsp;&nbsp;";
        }
    }

    for ($column = 1; $column <= $column_length; $column++) {
        // remove +1 if you want a sharp angle
        if ($column == ($column_length-$row)+1) {
            echo "*&nbsp;";
        } else {
            echo "&nbsp;&nbsp;&nbsp;";
        }   
    }
    echo "<br>";
}
?>

PHP program to print letter W with star pattern

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

Program Logic & Code
  • Take 10 rows and 11 columns, for the 6th column, will be the exact middle.
  • On the last row, print star in all columns, use this condition ($row == $row_length)
  • Print star at the first and last column of each row, use this condition ( $column == 1 || $column == $column_length)
  • Finally, between second and middle row, print star in middle column, use this condition ($row == $row_length/2 && $column == $column_middle)
  • If the condition does not satisfy, we print spaces.

<?php
$row_length = 10;
$column_length = 11;
$column_middle = ceil($column_length / 2);

for ($row = 1; $row <= $row_length; $row++) {
    for ($column = 1; $column <= $column_length; $column++) {
        if ($row == $row_length) {
            echo "*&nbsp;";
        } else if ($column == 1 || $column == $column_length) {
            echo "*&nbsp;";
        } else if ($column == $column_middle && $row >= ($row_length/2)) {
            echo "*&nbsp;";
        } else {
            echo "&nbsp;&nbsp;&nbsp;";
        }
    }
    echo "<br>";
}
?>

PHP program to print letter X with star pattern

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

Program Logic & Code
  • The program is divided into two parts, upper and lower sections.
  • The upper section is like letter V and the code logic is mentioned above.
  • The lower section is like an inverted letter V.

<?php
$row_length = 5;
$column_length = 5;
// upper section
for ($row = 1; $row <= $row_length; $row++) {

    for ($column = 1; $column <= $column_length; $column++) {

        if ($column == $row) {
            echo "*&nbsp;";
        } else {
            echo "&nbsp;&nbsp;&nbsp;";
        }
    }

    for ($column = 1; $column <= $column_length; $column++) {
        // remove +1 if you dont one a flat lower design
        if ($column == ($column_length-$row)+1) {
            echo "*&nbsp;";
        } else {
            echo "&nbsp;&nbsp;&nbsp;";
        }   
    }
    echo "<br>";
}
// lower section
for ($row = 1; $row <= $row_length; $row++) {

    for ($column = 1; $column <= $column_length; $column++) {
        // remove +1 if you dont one a flat lower design
        if ($column == ($column_length-$row)+1) {
            echo "*&nbsp;";
        } else {
            echo "&nbsp;&nbsp;&nbsp;";
        }   
    }
    for ($column = 1; $column <= $column_length; $column++) {

        if ($column == $row) {
            echo "*&nbsp;";
        } else {
            echo "&nbsp;&nbsp;&nbsp;";
        }
    }

    
    echo "<br>";
}
?>

PHP program to print letter Y with star pattern

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

Program Logic & Code
  • The program is divided into two parts.
  • The upper section is like letter V and the code logic is mentioned above.
  • The lower section, print star at the last column of each row.

<?php
$row_length = 5;
$column_length = 5;
for ($row = 1; $row <= $row_length; $row++) {

    for ($column = 1; $column <= $column_length; $column++) {

        if ($column == $row) {
            echo "*&nbsp;";
        } else {
            echo "&nbsp;&nbsp;&nbsp;";
        }
    }

    for ($column = 1; $column <= $column_length; $column++) {
        if ($column == ($column_length - $row)) {
            echo "*&nbsp;";
        } else {
            echo "&nbsp;&nbsp;&nbsp;";
        }
    }
    echo "<br>";
}

for ($row = 1; $row <= $row_length; $row++) {
    for ($column = 1; $column <= $column_length; $column++) {
        if ($column == $column_length) {
            echo "*&nbsp;";
        } else {
            echo "&nbsp;&nbsp;&nbsp;";
        }
    }
    echo "<br>";
}
?>

PHP program to print letter Z with star pattern

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

Program Logic & Code
  • Take 10 rows and 10 columns.
  • On the first and last row, print star in all columns, use this condition ($row == 1 || $row == $row_length)
  • From second till second-to-last row, print star at current row equals current row (descending order), use this condition ( $column <= $row_length -row )
  • If the condition does not satisfy, we print spaces.

<?php
$row_length = 10;
$column_length = 10;
for ($row = 1; $row <= $row_length; $row++) {
    for ($column = 1; $column <= $column_length; $column++) {
        if ($row == 1 || $row == $row_length) {
            echo "*&nbsp;";
        } else if ($column <= ($row_length - $row)) {
            echo "&nbsp;&nbsp;&nbsp;";
        } else {
            echo "*&nbsp;";
            break;
        }
    }
    echo "<br>";
}
?>

If you have any queries or you can code the program more efficiently, please let me know in the comments box.

Get the latest updates directly in your inbox for FREE: Subscribe here

Share your feedback / let me know your doubts regarding this tutorial in the comment box given below.

Share:
Hasan

Shahrukh Khan (Hasan)

A software engineer who's a die-hard coder, blogger, dreamer and mentor with years of expertise in web development. Know more...

Related Posts

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.