Star Pattern Programs in PHP – Set 2

by  
Share:
star-pattern-programs-php

Learn PHP star patterns programs to print different shapes like hollow right-angle triangle, hollow inverted right-angle triangle, mirror right-angle triangle, hollow inverted mirror right-angle triangle, acute triangle, etc.

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

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

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

Program Logic & Code
  • Base logic is already given in the program to print right-angle triangle with star pattern.
  • 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 5 times.
  • For the last row, print star in all column, use this condition ($row == 1 || $row == $row_length)
  • Between first and last row, print star in first and last column only, use this condition ($column == 1 || $column == $column_length)
  • Inside the inner loop print star.
  • If the condition does not satisfy, we print spaces.

<?php
$row_length = 5;
for ($row = 1; $row <= $row_length; $row++) {
    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 hollow inverted right-angle triangle with star pattern

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

Program Logic & Code
  • Base logic is already given in the program to print inverted right-angle triangle with star pattern.
  • 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 5 times.
  • For the first row, print star in all columns, use this condition ($row == 0)
  • Between first and second-to-last row, 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, we print spaces.

<?php
$row_length = 5;
for ($row = 0; $row < $row_length; $row++) {
    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 hollow mirror right-angle triangle with star pattern

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

Program Logic & Code
  • Base logic is already given in the program to print mirror right-angle triangle with star pattern.
  • Take 5 rows and 5 columns.
  • 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.
  • For the last row, print star in all columns, use this condition ($row == $row_length)
  • Between first and second-to-last row, 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, we print spaces.

<?php
$row_length = 5;
for ($row = 1; $row <= $row_length; $row++) {
    for ($spaces = 1; $spaces <= $row_length - $row; $spaces++) {
        echo "&nbsp;&nbsp;&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 hollow inverted mirror right-angle triangle with star pattern

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

Program Logic & Code
  • Base logic is already given in the program to print inverted mirror right-angle triangle with star pattern.
  • Take 5 rows and 5 columns.
  • 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.
  • For the first row, print star in all columns, use this condition ($row == 0)
  • Between first and second-to-last row, 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, we print spaces.

<?php
$row_length = 5;
for ($row = 0; $row < $row_length; $row++) {
    for ($spaces = 0; $spaces < $row; $spaces++) {
        echo "&nbsp;&nbsp;&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 acute triangle with star pattern – style #1

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

Program Logic & Code

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

PHP program to print acute triangle with star pattern – style #2

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

Program Logic & Code

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

PHP program to print multi-triangle with star pattern – style #1

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

Program Logic & Code

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

PHP program to print multi-triangle with star pattern – style #2

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

Program Logic & Code

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