Star Pattern Programs in PHP – Set 3

by  
Share:
star-pattern-programs-php

Learn PHP star patterns programs to print different shapes like pyramid, inverted pyramid, hollow pyramid, inverted hollow pyramid, full diamond, hollow diamond, hourglass, etc and related programs.

This article is a continuation of my last program Star pattern Programs in PHP – Set 2. If you are a beginner, I would recommend you read the article.

PHP program to print pyramid 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 into 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. Print space under this loop.
  • 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++) {
        echo "&nbsp;";
    }
    for ($column = 1; $column <= $row; $column++) {
        echo "*&nbsp;";
    }
    echo '<br>';
}
?>

PHP program to print hollow pyramid with star pattern

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

Program Logic & Code
  • Base logic is already given the above program
  • For the last row, print star in all columns, use this condition ($row == $row_length)
  • Between first and second-to-last rows, print star in first and last column only, use this condition ($column == 1 || $column == $row)
  • Inside the inner loop print star.
  • If the condition does not satisfy print spaces.

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

PHP program to print inverted pyramid with star pattern

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

Program Logic & Code
  • Take 5 rows and 5 columns.
  • The trick here is playing with spaces.
  • Observe, as the rows increase column decreases. See, Row 1 has 5 stars, Row 2 has 4 stars
  • Similarly, as rows increase spaces increases. See, Row 1 has 0 spaces, Row 2 has 1 space.
  • In the first inner loop print space and for the other inner loop print star.

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

PHP program to print hollow inverted pyramid with star pattern

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

Program Logic & Code
  • Base logic is already given the above program
  • For the first row, print star in all columns, use this condition ($row == 0)
  • Between first and second-to-last rows, print star in first and last column only, use this condition ($column == 0 || $column == ($row_length-$row)-1 )
  • Inside the inner loop print star.
  • If the condition does not satisfy print spaces.

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

PHP program to print hourglass with star pattern

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

Program Logic & Code
  • Break the program into two parts.
  • In the upper section, from Row 1 to Row 5, logic is the same as printing inverted pyramid with star pattern (given above).
  • In the right section, from Row 6 to Row 10, logic is the same as printing pyramid with star pattern (given above).

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

PHP program to print hollow diamond with star pattern

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

Program Logic & Code
  • Break the program into two parts.
  • In the upper section, from Row 1 to Row 5, logic is the same as printing hollow pyramid with star pattern (given above).
  • In the right section, from Row 6 to Row 9, logic is the same as printing hollow inverted pyramid with star pattern (given above).

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

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

PHP program to print full diamond with star pattern

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

Program Logic & Code

<?php
$row_length = 5;

// upper section
for ($row = 0; $row < $row_length; $row++) {
    //left section
    for ($column = 1; $column <= $row_length - $row; $column++) {
        echo "*&nbsp;";
    }
    
    for ($spaces = 0; $spaces < $row; $spaces++) {
        echo "&nbsp;&nbsp;&nbsp;";
    }

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

// lower section
for ($row = 2; $row <= $row_length; $row++) {
    // left section
    for ($column = 1; $column <= $row; $column++) {
        echo "*&nbsp;";
    }
    
    for ($spaces = 0; $spaces <  $row_length - $row; $spaces++) {
        echo "&nbsp;&nbsp;&nbsp;";
    }
    
    // right section
    for ($spaces = 1; $spaces <= $row_length - $row; $spaces++) {
        echo "&nbsp;&nbsp;&nbsp;";
    }
    for ($column = 1; $column <= $row; $column++) {
        echo "*&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

Leave a Reply

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