Skip to content
Have a Question?
Table of Contents
< All Topics
Print

PHP Star Pattern

In this article, we’ll learn how to make a star pattern in PHP. The interviewer frequently asks you to draw a star pattern in PHP to demonstrate your understanding of loops.

If you don’t know about loops, you can learn more about PHP loops here.


Star Pattern In PHP

In PHP, we’ll be using for loops to create start patterns. Take note of the following:

  • <br> tag to add a line break
  • And &nbsp; to add a space

Here is the top 10-star pattern in PHP:

1. Star Pattern 1

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

2. Star Pattern 2

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

3. Star Pattern 3

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

4. Star Pattern 4

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

5. Star Patten 5

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

6. Star Pattern 6

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

7. Star Pattern 7

    *        
   * *      
  *   *    
 *     *  
*       *

8. Star Patten 8

*       *
 *     *  
  *   *    
   * *      
    * 

9. Star Pattern 9

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

10. Star Pattern 10

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

11. Star Patten 11

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

12. Star Pattern 12

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

13. Star Pattern 13

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

14. Star Patten 14

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

15. Star Pattern 15

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

Let’s create them one by one.


1. Rectangle Shape Star Pattern in PHP

In general, a rectangle pattern is very easy to create. We can create this shape (either rectangle or square) using the number of rows or columns.

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

To create this rectangle shape star pattern, we need two nested loops. The outer loop will print the row and the inner loop will print the * (star).

In our case, the number of rows is 5. So we need to repeat the outer loop 5 times. And inside the loops, we’ve to repeat the inner loop 10 times as our star pattern has 10 columns.

Here’s how to print this rectangle shape star pattern in PHP.

<?php
    // rectangle pattern
    $rows = 5;
    $columns = 10;
    
    for($i = 0; $i < $rows; $i++) {
        // print column
        for($j = 0; $j < $columns; $j++) {
          // print row
          echo "*";
        }
        // change line
        echo "\n"; // or use "<br/>"
    }
    
?>

Note that, we’ve used \n to print a new line. This is called an escape character.


2. Left triangle star pattern

This pattern is commonly used in practice. To create the left triangle star pattern in PHP, let’s understand it first.

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

Here, the rows are 5 and the number of rows is equal to the star in each row. So, we’ve to repeat the inner loop for times equal to the index of the outer loop i.e for rows and print a star (*).

And at the end of each row, print a new line using an escape character (\n) or <br>.

<?php
    // Left triangle star pattern
    $rows = 5;
    
    for($i = 0; $i < $rows; $i++) {
        // print column
        for($j = 0; $j <= $i; $j++) {
          // print row
          echo "*";
        }
        // change line
        echo "\n"; // or use "<br/>"
    }
    
?>

3. Right triangle star pattern in PHP

This is another triangle star pattern. See the above star pattern. The only difference is, it contains 4 spaces in the first row, 3 spaces in 2nd row and so on.

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

Here’re the steps to print this pattern in PHP.

  • To print columns, iterate the outer loop 5 times.
  • Print a star (*) inside this loop after repeating the inner loop a number of times equal to the triangle’s size.
  • Print a new line with the \n or <br> at the end of each row.
<?php
    // Right triangle star pattern
    $rows = 5;
    
    for($i = 0; $i < $rows; $i++) {
        // print Space
        for($j = 1; $j < $rows - $i; $j++) {
            echo  "&nbsp;&nbsp;";
        }
        
        // print stars
        for($k = 0; $k <= $i; $k++) {
            echo "*";
        }
        
        // change line
        echo "\n"; // or use "<br/>"
    }
    
?>

4. Left triangle downward star pattern in PHP

*****
 ****
  ***
   **
    *
<?php
    // downward left triangle pattern
    $rows = 5;

    for($i = 0; $i <  $rows; $i++) {
        // print spaces
        
        for($j= 1; $j <= $i; $j++) {
            echo  "&nbsp;&nbsp;"; 
        }
        //print stars
        for($k = 0; $k <  $rows - $i; $k++) {
            echo "*";
        }
        echo "<br>";
    }
?>

Explanation of the above code:

The outer loop for($i = 0; $i < $rows; $i++) is used to control the number of rows in the pattern. The first inner loop for($j= 1; $j <= $i; $j++) is used to print the spaces before each row of stars, and the number of spaces is equal to the current row number, so it starts from 1 and goes up to $i.

The second inner loop for($k = 0; $k < $rows - $i; $k++) is used to print the stars, and the number of stars is equal to the total number of rows minus the current row number. so it starts from 0 and goes up to $rows-$i


5. Pyramid(an isosceles triangle) Star Pattern In PHP

    *
   ***
  *****
 *******
*********
<?php
    
    $rows = 5;
    
    for($i = 0; $i < $rows; $i++) {
        // print spaces
        for($j = 0; $j < $rows - $i - 1; $j++) {
            echo "&nbsp;&nbsp;";
        }
        // print stars
        for($k = 0; $k < 2 * $i + 1; $k++) {
            echo "*";
        }
        echo "<br>";
    }
?>

Explanation of the above code:

The outer loop for($i = 0; $i < $rows; $i++) is used to control the number of rows in the pattern. The first inner loop for($j = 0; $j < $rows - $i - 1; $j++) is used to print the spaces before each row of stars, and the number of spaces is equal to the total number of rows minus the current row number minus 1.

The second inner loop for($k = 0; $k < 2 * $i + 1; $k++) is used to print the stars, and the number of stars is equal to twice the current row number plus 1.


6. Downward Pyramid Star Pattern In PHP

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

<?php
    
    $rows = 5;
    
    for($i = 0; $i < $rows; $i++) {
        // print spaces
        for($j = 0; $j < $i; $j++) {
            echo "&nbsp;&nbsp;";
        }
        // print stars
        for($k = 0; $k < 2 * ($rows - $i) - 1; $k++) {
            echo "*";
        }
        echo "<br>";
    }
?>

Explanation of the above code:

The outer loop for($i = 0; $i < $rows; $i++) is used to control the number of rows in the pattern. The first inner loop for($j = 0; $j < $i; $j++) is used to print the spaces before each row of stars, and the number of spaces is equal to the current row number.

The second inner loop for($k = 0; $k < 2 * ($rows - $i) - 1; $k++) is used to print the stars, and the number of stars is calculated by multiplying twice the difference between total number of rows and current row number, and subtracting 1 from the result.

Note that, you should also be aware that the use of &nbsp;&nbsp; to create spaces will only work when displaying the output on a web page. 
If you want to print the pattern in the console or store it in a file, you should use a different method to create spaces, such as concatenating strings of empty spaces or an escape character.

7. Alphabet like A

    *        
   * *      
  *   *    
 *     *  
*       *
<?php
     for($i=1; $i<=5; $i++) {
        for($j=5; $j>=1; $j--) {
            if($i == $j) {
               echo "*";
            } else {
                echo "&nbsp;&nbsp;";   
            }
             
         }
       for($k=2; $k<=5; $k++) {
          if($i == $k) {
            echo "*";
          } else {
            echo "&nbsp;&nbsp;";
          }  
        }
       echo "<br>";
      }
?>

Explanation of the above code:

The outer loop for($i=1; $i<=5; $i++) is used to control the number of rows in the pattern.

The first inner loop for($j=5; $j>=1; $j--) is used to print the spaces and stars on the left side of the diamond shape pattern.

It starts with the total number of rows and decrements by 1 for each iteration. If the current row number is equal to the current iteration of the inner loop, then it prints a star, otherwise, it prints two spaces.

The second inner loop for($k=2; $k<=5; $k++) is used to print the spaces and stars on the right side of the diamond shape pattern. It starts with 2 and increments by 1 for each iteration.

If the current row number is equal to the current iteration of the inner loop, then it prints a star, otherwise, it prints two spaces.


8. V shape Star Pattern In PHP

*       *
 *     *  
  *   *    
   * *      
    * 
<?php
     for($i=5; $i>=1; $i--) {
        for($j=5; $j>=1; $j--) {
            if($i == $j) {
               echo "*";
            } else {
                echo "&nbsp;&nbsp;";   
            }
             
         }
       for($k=2; $k<=5; $k++) {
          if($i == $k) {
            echo "*";
          } else {
            echo "&nbsp;&nbsp;";
          }  
        }
       echo "<br>";
      }
?>

Explanation of the above code:

The outer loop for($i=5; $i>=1; $i--) is used to control the number of rows in the pattern and it starts with the total number of rows and decrements by 1 for each iteration.

The first inner loop for($j=5; $j>=1; $j--) is used to print the spaces and stars on the left side of the diamond shape pattern.

It starts with the total number of rows and decrements by 1 for each iteration. If the current row number is equal to the current iteration of the inner loop, then it prints a star, otherwise, it prints two spaces.

The second inner loop for($k=2; $k<=5; $k++) is used to print the spaces and stars on the right side of the diamond shape pattern.

It starts with 2 and increments by 1 for each iteration. If the current row number is equal to the current iteration of the inner loop, then it prints a star, otherwise, it prints two spaces.


9. Hollow Square Star Pattern In PHP

*****
*   *
*   *
*   *
*****
<?php

    $rows = 5;
    for($i = 0; $i < $rows; $i++) {
        // print column
        for($j = 0; $j < $rows; $j++) {
            // print only star in first and last row
            if($i === 0 || $i === $rows - 1) {
                echo "*";
            }
            else {
                // Now print star only in first and last position row
                if($j === 0 || $j === $rows - 1) {
                    echo "*";
                }
                else {
                    // use &nbsp; for space
                    echo "&nbsp;&nbsp;";
                }
            }
        }
        echo "<br>"; // Use <br> tag for line break
    }
?>

Explanation of the above code:

The outer loop for($i = 0; $i < $rows; $i++) is used to control the number of rows in the pattern. It starts with 0 and increments by 1 for each iteration.

The inner loop for($j = 0; $j < $rows; $j++) is used to control the number of columns in the pattern. It starts with 0 and increments by 1 for each iteration.

Inside the inner loop, there are two if-else conditions, the first one if($i === 0 || $i === $rows - 1) checks if the current row is the first or last row, if true it will print a star.

The second if-else condition if($j === 0 || $j === $rows - 1) checks if the current column is the first or last column, if true it will print a star. Otherwise, it will print two spaces using echo "&nbsp;&nbsp;";

You can also use escape character to display the spaces.


10. Diamond Star Pattern in PHP

    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *
<?php

     $rows = 5;
       // First Part( Upside)
     for($i=1; $i <= $rows; $i++) {
         for($j=$i; $j<5; $j++) {
           echo "&nbsp;&nbsp;";
         }
        for($k=1; $k<$i*2; $k++) {
           echo "*";
        }
         echo "<br>";
 
     }
      // Second Part DownWard
    for($i=4; $i>=1; $i--) {
       for($j=5; $j>$i; $j--) {
         echo "&nbsp;&nbsp;";
       }
      for($k=1; $k<$i*2; $k++) {
        echo "*";
       }
     echo "<br>";
    }
?>

Explanation of the above code:

The outer loop for($i=1; $i <= $rows; $i++) is used to control the number of rows in the first part of the pattern which is upward.

The inner loop for($j=$i; $j<5; $j++) is used to print spaces before each row starts. It starts with the current value of $i and increment by 1 for each iteration, this will print the spaces before the stars. The innermost loop for($k=1; $k<$i*2; $k++) is used to print the stars.

After the first part of the pattern, there is another outer loop for($i=4; $i>=1; $i--) is used to control the number of rows in the second part of the pattern which is downward.

The inner loop for($j=5; $j>$i; $j--) is used to print spaces before each row starts. It starts with 5 and decrement by 1 for each iteration, this will print the spaces before the stars.

The innermost loop for($k=1; $k<$i*2; $k++) is used to print the stars.


11. Reverse Downward Triangle Star Pattern In PHP

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

It’s pretty simple to print this star pattern in PHP. We’ve to print the 9 rows and stars in a decremented fashion. Here’s how we did.

Steps to be followed:

  • To print rows, create an external loop to run 9 times.
  • The inner loop will print columns, then repeat the inner loop a number of times equal to size minus the outer loop index and print a star (*). It will print the star in a decremented fashion.
  • And then, we’ve to print the new line using <br> at the end of each row.
<?php
    // Reverse downward triangle pattern
    $rows = 9;
    for($i = 0; $i < $rows; $i++) {
        // print stars
        for($j = 0; $j < $rows - $i; $j++) {
            echo "*";
        }
        echo "<br>";
    }
?>

12. Hollow Diamond Star Pattern In PHP

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

This star pattern is a combination of star patterns like the alphabet A and V-shape. (Star Pattern 7 & 8)

The below code will print the star pattern in PHP.

<?php
    $rows = 5;
    
    // First part pyramid
    for ($i = 1; $i <= $rows; $i++) {
    
        // printing spaces using &nbsp;
        for ($j = $rows; $j > $i; $j--) {
            echo "&nbsp;&nbsp;";
        }
        
        // printing star
        for ($k = 0; $k < $i * 2 - 1; $k++) {
            if ($k === 0 || $k === 2 * $i - 2) {
                echo "*";
            }
            else {
                echo "&nbsp;&nbsp;";
            }
        }
        echo "<br>";
    }
    // Second part pyramid
    for ($i = 1; $i <= $rows - 1; $i++) {
        // printing spaces using &nbsp;
        for ($j = 0; $j < $i; $j++) {
            echo "&nbsp;&nbsp;";
        }
        // printing star *
        for ($k = ($rows - $i) * 2 - 1; $k >= 1; $k--) {
            if ($k === 1 || $k === ($rows - $i) * 2 - 1) {
                echo "*";
            }
            else {
                echo "&nbsp;&nbsp;";
            }
        }
        echo "<br>";
    }
?>

13. Heart Shape Star Pattern in PHP

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

Here’s how we create this heart shape star pattern in PHP.

<?php
    // heart star pattern in PHP
    $rows = 6;
    
    for ($i = $rows / 2; $i < $rows; $i += 2) {
        // print first spaces using &nbsp;
        for ($j = 1; $j < $rows - $i; $j += 2) {
            echo "&nbsp;&nbsp;";
        }
        // print first stars *
        for ($j = 1; $j < $i + 1; $j++) {
            echo "*";
        }
        // print second spaces using &nbsp;
        for ($j = 1; $j < $rows - $i + 1; $j++) {
            echo "&nbsp;&nbsp;";
        }
        // print second stars *
        for ($j = 1; $j < $i + 1; $j++) {
            echo "*";
        }
        echo "<br>";
    }
    // lower part
    // inverted pyramid code
    for ($i = $rows; $i > 0; $i--) {
        for ($j = 0; $j < $rows - $i; $j++) {
            echo "&nbsp;&nbsp;";
        }
        for ($j = 1; $j < $i * 2; $j++) {
            echo "*";
        }
        echo "<br>";
    }
?>

Explanation of the above code:

The first set of nested loops creates the upper part of the heart shape. It starts with the variable $i being set to half the total number of rows, and increments by 2 in each iteration.

It then we use two nested loops to print spaces (using the   html character) and stars in a specific pattern to create the shape.

The second set of loops creates the lower part of the heart shape, which is an inverted pyramid pattern. It starts with the variable $i being set to the total number of rows, and decrements by 1 in each iteration.

It then uses nested loops to print spaces and stars in a specific pattern to create the inverted pyramid shape. The last echo “<br>” is used to create a new line after each iteration.


14. Right Pascal Star Pattern In PHP

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

It’s pretty simple as it is the combination of the left triangle and their downward reverse. Here’s how to create this star pattern in PHP.

The first section will create rows and print stars in an incremented fashion, and the second section will print stars in a decrement fashion.

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

15. Left Pascal Star Pattern In PHP

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

It’s pretty simple as it is the combination of a right triangle and in reverse downward. Here’s how to create this star pattern in PHP.

The first section will create rows and print stars in an incremented fashion with spaces, and the second section will print stars in a decrement fashion with spaces.

<?php
    $rows = 5;
    //First part
    for ($i = 1; $i <= $rows; $i++) {
        for ($j = 0; $j < $rows - $i; $j++) {
            echo "&nbsp;&nbsp;";
        }
        for ($k = 0; $k < $i; $k++) {
            echo "*";
        }
        echo "<br>";
    }

     //Second Part
    for ($i = 1; $i <= $rows - 1; $i++) {
        for ($j = 0; $j < $i; $j++) {
            echo "&nbsp;&nbsp;";
        }
        for ($k = 0; $k < $rows - $i; $k++) {
            echo "*";
        }
        echo "<br>";
    }
?>

That’s all about the star pattern in PHP. That’s usually asked by the interviewer to test your knowledge of PHP loops.

Hope it was enjoyable. Please feel free to comment below with your thoughts. It greatly inspires us to create this kind of lovely stuff.

Also, you may subscribe to our Youtube Channel for amazing tech content and other stuff.


Reminder

Hi Developers, We are working to cover every single concept in PHP Complete Series with examples for quick and easy learning. Last but not least, kindly

Kindly do a google search for:

Happy learning bravo!

Share your feedback

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

Copyright © 2021-2024 | Fokat Guru | Managed by Fokat Guru