Star Pattern Programs in PHP – Set 1

by  
Share:
star-pattern-programs-php

In this tutorial, you will learn how to print different shapes with star pattern in PHP. These kinds of short questions are always asked in interviews, people who are quick to solve these problems have a great understanding of loops, 2D/3D dimensional array. The logic used here can be used to solve the problems in any other programming language like C, C++, Java, etc.

PHP program to print Square with star pattern

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

Program Logic & Code
  • Take 5 rows and 5 columns.
  • Start an outer loop (row) that iterate 5 times.
  • Under the outer loop start an inner loop (column) that iterate the same number of times.
  • Inside the inner loop print star.

<?php
$row_length = 5; $column_length = 5;
for ($row = 1; $row <= $row_length; $row++) {
    for ($column = 1; $column <= $column_length; $column++) {
       // I have used an extra space for pretty formatting. 
        echo "*&nbsp;";
    }
    echo '<br>';
}
?>

PHP program to print Hollow Square with star pattern

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

Program Logic & Code
  • Take 5 rows and 5 columns.
  • Start an outer loop (row) that iterate 5 times.
  • Under the outer loop start an inner loop (column) that iterate the same number of times.
  • On the first and last row, print star in all column, use this condition ($row == 1 || $row == $row_length)
  • From second row till second-to-last row, print star in the first and last column only, use this condition ($column == 1 || $column == $column_length)
  • Inside the inner loop print star.

<?php
$row_length = 5; $column_length = 5;
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 Rectangle with star pattern

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

Program Logic & Code
  • Take 5 rows and 10 columns.
  • Start an outer loop (row) that iterate 5 times.
  • Under the outer loop start an inner loop (column) that iterate 10 times.
  • Inside the inner loop print star.

<?php
$row_length = 5; $column_length = 10;
for ($row = 1; $row <= $row_length; $row++) {
    for ($column = 1; $column <= $column_length; $column++) {
       // I have used an extra space for pretty formatting. 
        echo "*&nbsp;";
    }
    echo '<br>';
}
?>

PHP program to print Hollow Rectangle with star pattern

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

Program Logic & Code
  • The base logic of this program is the same as the printing rectangle (given above).
  • On the first and last row, print star in all columns, use this condition ($row == 1 || $row == $row_length)
  • From second row till last row, print star in the first and last column only, use this condition ( $column == 1 || $column == $column_length)
  • Inside the inner loop print star.

<?php
$row_length = 5;
$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 right-angle triangle with star pattern

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

Program Logic & Code
  • Take 5 rows and 5 columns.
  • Observe, as the rows increase column value also increases. See, Row 2 has 2 stars, Row 3 has 3 stars
  • Start an outer loop (row) that iterate 5 times.
  • Under outer loop start an inner loop (column) that iterate till the current row value.
  • Inside the inner loop print star.

<?php
$row_length = 5;
for ($row = 1; $row <= $row_length; $row++) {
    for ($column = 1; $column <= $row; $column++) {
        echo "*&nbsp;";
    }
    echo '<br>';
}
?>

PHP program to print inverted right-angle triangle with star pattern

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

Program Logic & Code
  • Take 5 rows and 5 columns.
  • Observe, as the rows increase column value decreases. See, Row 1 has 5 stars, Row 2 has 4 stars
  • Start an outer loop (row) that iterate 5 times.
  • Under the outer loop start an inner loop (column) that iterate till row length minus current row value.
  • Inside the inner loop print star.

<?php
$row_length = 5;
for ($row = 0; $row < $row_length; $row++) {
    for ($column = 0; $column < $row_length - $row; $column++) {
        echo "*&nbsp;";
    }
    echo '<br>';
}
?>

PHP program to print mirror right-angle triangle with star pattern

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

Program Logic & Code
  • Take 5 rows and 5 columns.
  • Observe, as the rows increase column value increases. See, Row 1 has 1 star, Row 2 has 2 stars
  • Additionally, blank spaces in the grid will come in play here. As rows increase spaces decreases. See, Row 1 has 4 spaces, Row 2 has 3 spaces.
  • Start an outer loop (row) that iterate 5 times
  • Under the outer loop start an inner loop (for spaces) that iterate till total row length minus current row value.
  • After the inner loop for spaces, start loop again (for star) that iterate till current row value. Print star inside this loop.

<?php
$row_length = 5;
for ($row = 1; $row <= $row_length; $row++) {
    for ($spaces = 1; $spaces <= $row_length - $row; $spaces++) {
        // 3 spaces, 2 spaces for character used below and one additional space
        echo "&nbsp;&nbsp;&nbsp;";
    }
    for ($column = 1; $column <= $row; $column++) {
        // using one extra space to pretty output
        echo "*&nbsp;";
    }
    echo '<br>';
}
?>

PHP program to print inverted mirror right-angle triangle with star pattern

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

Program Logic & Code
  • Take 5 rows and 5 columns.
  • Observe, as the rows increase column value decreases. See, Row 1 has 5 stars, Row 2 has 4 stars
  • Additionally, blank spaces in the grid will come in play here. As rows increase spaces increases. See, Row 1 has 0 space, Row 2 has 1 space.
  • Start an outer loop (row) that iterate 5 times
  • Under the outer loop, start an inner loop (for spaces) that iterate till the current row value.
  • After the inner loop for spaces, start loop again (for star) that iterate till total row length minus current row value. Print star inside this loop.

<?php
$row_length = 5;
for ($row = 0; $row < $row_length; $row++) {
    for ($spaces = 0; $spaces < $row; $spaces++) {
        // 3 spaces, 2 spaces for character used below and one additional space
        echo "&nbsp;&nbsp;&nbsp;";
    }
    for ($column = 0; $column < $row_length - $row; $column++) {
        // using one extra space to pretty output
        echo "*&nbsp;";
    }
    echo '<br>';
}
?>

PHP program to print rhombus with star pattern

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

Program Logic & Code
  • Take 5 rows and 5 columns.
  • Observe, blank spaces in the grid will come in play here. As rows increase spaces decreases. See, Row 1 has 5 spaces, Row 2 has 4 spaces.
  • Start an outer loop (row) that iterate 5 times
  • Under the outer loop, start an inner loop (for spaces) that iterate till total row length minus current row value.
  • After the inner loop for spaces, start loop again (for star) that iterate till total row length. Print star inside this loop.

<?php
$row_length = 5;
for ($row = 0; $row < $row_length; $row++) {
    for ($spaces = 1; $spaces <= $row_length - $row; $spaces++) {
        echo "&nbsp;";
    }
    for ($column = 1; $column <= $row_length; $column++) {
        echo "*&nbsp;";
    }
    echo '<br>';
}
?>

PHP program to print hollow rhombus with star pattern

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

Program Logic & Code
  • The base logic of this program is the same as the printing rhombus (given above).
  • On the first and last row, print star in all columns, use this condition ($row == 1 || $row == $row_length)
  • From middle row till second-to-last row, print star at first and last column, use this condition ($column == 1 && $column == $column_length)
  • If the condition does not satisfy, we print spaces.

<?php
$row_length = 5;
for ($row = 0; $row < $row_length; $row++) {
    for ($spaces = 1; $spaces <= $row_length - $row; $spaces++) {
        echo "&nbsp;";
    }
    for ($column = 1; $column <= $row_length; $column++) {
        if ($column == 1 || $column == $row_length) {
            echo "*&nbsp;";
        } else if ($row == 0 || $row == $row_length - 1) {
            echo "*&nbsp;";
        } else {
            echo "&nbsp;&nbsp;&nbsp;";
        }
    }
    echo '<br>';
}
?>

 

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

Sam shaikh on

Nice Great Article sir

Reply
halith on

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

how to print like this

Reply
Shahrukh Khan on

I will be updating that soon.

Reply
Abhishek on

pattern should be dynamic

Reply
Shahrukh Khan on

Pattern is dynamic my friend, you just need to change the rows number.

Reply
sandeep kuamr on

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

Reply

Leave a Reply

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