PHP Programs to Print Pyramid Star Patterns

In this tutorial, learn how PHP programs print pyramid star patterns. Create simple, triangle and square patterns of star or asterisk with PHP. The various types of patterns using star given with explanation.

First of all the pattern examples are useful for interview questions of PHP. If you are looking for a job in PHP language. Due to this, you have to learn creating these pattern to prepare for the interview. Interviewers may ask questions based on creating patterns in PHP.

Therefore, learn carefully each pattern and remember the steps. Practice each example and understand how you can create patterns.

Print Pyramid Star Simple Pattern in PHP

To create a simple star pattern using PHP, you have to use two PHP for loop. Due to this, nested for loop define rows to create with the columns of stars.

The first for loop define the number of rows for the simple pattern. The second loop prints each row with the star symbols. Here, you have to define the for condition as $j<=$i. You are almost done with the addition of this to the below code.

Output

*

**

***

****

*****

******

Finally, the above example showing the simple pattern. The pattern showing an increasing number of stars in each row. As a result, up to six rows showing the stars for a simple pattern using PHP.

180 Degree Rotation Simple Star Pattern in PHP

Similarly, if you want to create 180 degrees rotated simple pyramid pattern. Use the below example which prints the decreasing number of stars in each row. The stars start with six in number at the first row.

Output

******

*****

****

***

**

*

You may be increased or decrease the starting number of stars in the first row. As a result, you will get 180 rotated pattern of simple star pyramid.

Triangle Pyramid Star Pattern in PHP

To create a triangle pattern program in PHP using stars. You have to define the size of the pattern at the top of the code. The size is the number of rows to create a pyramid shape pattern triangle in PHP using for loop.

Check the below method to create your own triangle pattern in PHP.

Output

    *

   * *

  * * *

 * * * *

* * * * *

Inside loop for numbers of rows, you have to define two more for loops. The inner first for loop put spaces before the stars in the triangle pattern program in PHP. Likewise, the second loop adds stars after the above loop added spaces. Finally, the overall code creates a triangle patterns shape of stars using PHP.

Square Star Pattern in PHP

In addition to above all, you can create a square shape pattern of stars using PHP. Square is another pyramid pattern, you have to create rows of stars. So, in each creation of rows, add the same number of stars to each row. Therefore, add four stars for both rows and columns and you are almost done for square.

Output

****

****

****

****

Probably, you want to create more patterns or learn more shapes using PHP. Create even more patterns of squares and prepare for the PHP interview questions of pyramid stars.

Certainly, practicing each pattern many times is the main required thing. Students need to write each code many times in a notebook to remember and crack each interview.

Do even more practice by learning pyramids of numbers using PHP.