Print numbers with star pattern in PHP

by  
Share:
print-numbers-star-pattern-programs-php

Learn PHP star patterns programs to print different numbers from zero to nine (0-9) with star pattern. The logic used here can be used to solve the same problems in any other programming language like C, C++, Java, etc.

 

PHP program to print number one with star pattern

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

Program Logic & Code

  • Take 10 rows and 11 columns, for the 6th column, will be the exact middle.
  • From first row, print star in from first to middle column only, use this condition ( $row == 1 && $column < $column_middle)
  • From second till second-to-last row, print star in the middle column only, use this condition ($column == $column_middle)
  • Finally, on the last row, print star in all columns, use this condition ($row == $row_length)
  • If the condition does not satisfy, we print spaces.
<?php
$row_length = 10;
$column_length = 11;
$column_middle = ceil($column_length / 2);
for ($row = 1; $row <= $row_length; $row++) {
    for ($column = 1; $column <= $column_length; $column++) {
        if ($row == 1 && $column < $column_middle) {
            echo "*&nbsp;";
        } else if ($column == $column_middle) {
            echo "*&nbsp;";
        } else if ($row == $row_length) {
            echo "*&nbsp;";
        } else {
            echo "&nbsp;&nbsp;&nbsp;";
        }
    }
    echo "<br>";
}
?>

PHP program to print number two with star pattern

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

Program Logic & Code

  • Take 11 rows and 10 columns, for the 6th row, will be the exact middle.
  • On the first, middle and last row, print star in all columns, use this condition ($row == 1 || $row == $row_middle || $row == $row_length)
  • From second row till middle row, print star in the last column only, use this condition ( $column == $column_length && $row < $row_middle)
  • Also, from middle to second-to-last row, print star in the first column only, use this condition ( $row > $row_middle && $column == 1)
  • If the condition does not satisfy, we print spaces.
<?php
$row_length = 11;$column_length = 10;
$row_middle = ceil($row_length / 2);
for ($row = 1; $row <= $row_length; $row++) {
    for ($column = 1; $column <= $column_length; $column++) {
        if ($row == 1 || $row == $row_middle || $row == $row_length) {
            // this is for the first, middle and last row
            echo "*&nbsp;";
        } else if ($column == $column_length && $row < $row_middle) {
                // for the last column on the upper section
                echo "*&nbsp;";
            } else if ($column == 1 && $row > $row_middle) {
                // for the first column on the lower section
                echo "*&nbsp;";
            } else {
                echo "&nbsp;&nbsp;&nbsp;";
            }
    }
    echo "<br>";
}
?>

PHP program to print number three with star pattern

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

Program Logic & Code

  • Take 11 rows and 10 columns, for the 6th row, will be the exact middle.
  • On the first, middle and last row, print star in all columns, use this condition ($row == 1 || $row == $row_middle || $row == $row_length)
  • From second row till second-to-last row, print star in the last column only, use this condition ( $column == $column_length)
  • If the condition does not satisfy, we print spaces.
<?php
$row_length = 11; $column_length = 10; 
$row_middle = ceil($row_length / 2);
for ($row = 1; $row <= $row_length; $row++) {
    for ($column = 1; $column <= $column_length; $column++) {
        if ($row == 1 || $row == $row_middle || $row == $row_length ) {
            echo "*&nbsp;";
        } else if ($column == $column_length) {
            echo "*&nbsp;";
        } else {
            echo "&nbsp;&nbsp;&nbsp;";
        }
    }
    echo "<br>";
}
?>

PHP program to print number four with star pattern

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

Program Logic & Code

  • Take 11 rows and 10 columns, for the 6th row, will be the exact middle.
  • On the middle row, print star in all columns, use this condition ($row == $row_middle)
  • From first row till middle row, print star in the first column only, use this condition ( $column == 1 && $row <= $row_middle)
  • Print star at last column of each row, use this condition ($column == $column_length)
  • If the condition does not satisfy, we print spaces.
<?php
$row_length = 11;
$column_length = 10;
$row_middle = ceil($row_length / 2);
for ($row = 1; $row <= $row_length; $row++) {
    for ($column = 1; $column <= $column_length; $column++) {
        if ($column == 1 && $row <= $row_middle) {
            echo "*&nbsp;";
        } else if ($row == $row_middle) {
            echo "*&nbsp;";
        } else if ($column == $column_length) {
            echo "*&nbsp;";
        } else {
            echo "&nbsp;&nbsp;&nbsp;";
        }
    }
    echo "<br>";
}
?>

PHP Program to print number five with star pattern

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

Program Logic & Code

  • Take 11 rows and 10 columns, for the 6th row, will be the exact middle.
  • On the first, middle and last row, print star in all columns, use this condition ($row == 1 || $row == $row_middle || $row == $row_length)
  • From second row till middle row, print star in the first column only, use this condition ( $column == 1 && $row < $row_middle)
  • Also, from middle to second-to-last row, print star in the last column only, use this condition ( $row > $row_middle && $column == $column_length)
  • If the condition does not satisfy, we print spaces.
<?php
$row_length = 11;
$column_length = 10;
$row_middle = ceil($row_length / 2);

for ($row = 1; $row <= $row_length; $row++) {
    for ($column = 1; $column <= $column_length; $column++) {
        if ($row == 1 || $row == $row_middle || $row == $row_length) {
            echo "*&nbsp;";
        } else if ($column == 1 && $row <= $row_middle) {
            echo "*&nbsp;";
        } else if ($column == $column_length && $row >= $row_middle) {
            echo "*&nbsp;";
        } else {
            echo "&nbsp;&nbsp;&nbsp;";
        }
    }
    echo "<br>";
}
?>

PHP program to print number six with star pattern

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

Program Logic & Code

  • Take 11 rows and 10 columns, for the 6th row, will be the exact middle.
  • On the first, middle and last row, print star in all columns, use this condition ($row == 1 || $row == $row_middle || $row == $row_length)
  • From second row till last row, print star in the first column only, use this condition ( $column == 1)
  • Also, from middle to second-to-last row, print star in the last column only, use this condition ( $row > $row_middle && $column == $column_length)
  • If the condition does not satisfy, we print spaces.
<?php
$row_length = 11;
$column_length = 10;
$row_middle = ceil($row_length / 2);

for ($row = 1; $row <= $row_length; $row++) {
    for ($column = 1; $column <= $column_length; $column++) {
        if ($row == 1 || $row == $row_middle || $row == $row_length) {
            // for the first, middle and last row
            echo "*&nbsp;";
        } else if ($column == 1) {
            // for the first column
            echo "*&nbsp;";
        } else if ($column == $column_length && $row > $row_middle) {
            // last column, but between middle and last column
            echo "*&nbsp;";
        } else {
            echo "&nbsp;&nbsp;&nbsp;";
        }
    }
    echo "<br>";
}
?>

PHP program to print number seven with star pattern

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

Program Logic & Code

  • Take 10 rows and 10 columns.
  • On the first row, print star in all columns, use this condition ($row == 1)
  • Print star at last column of each row, use this condition ($column == $column_length)
  • If the condition does not satisfy, we print spaces.
<?php
$row_length = 10;
$column_length = 10;
for ($row = 1; $row <= $row_length; $row++) {
    for ($column = 1; $column <= $column_length; $column++) {
        if ($row == 1) {
            echo "*&nbsp;";
        } else if ($column == $column_length) {
            echo "*&nbsp;";
        } else {
            echo "&nbsp;&nbsp;&nbsp;";
        }
    }
    echo "<br>";
}
?>

Program to print number eight with star pattern

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

Program Logic & Code

  • Take 11 rows and 10 columns, for the 6th row, will be the exact middle.
  • On the first, middle and last row, print star in all columns, use this condition ($row == 1 || $row == $row_middle || $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)
  • If the condition does not satisfy, we print spaces.
<?php
$row_length = 11;
$column_length = 10;
$row_middle = ceil($row_length / 2);

for ($row = 1; $row <= $row_length; $row++) {
    for ($column = 1; $column <= $column_length; $column++) {
        if ($row == 1 || $row == $row_middle || $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 number nine with star pattern

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

Program Logic & Code

  • Take 11 rows and 10 columns, for the 6th row, will be the exact middle.
  • On the first, middle and last row, print star in all columns, use this condition ($row == 1 || $row == $row_middle || $row == $row_length)
  • From second row till middle row, print star in the first column only, use this condition ( $column == 1 && $row < $row_middle)
  • Print star at last column of each row, use this condition ( $column == $column_length)
  • If the condition does not satisfy, we print spaces.
<?php
$row_length = 11;
$column_length = 10;
$row_middle = ceil($row_length / 2);

for ($row = 1; $row <= $row_length; $row++) {
    for ($column = 1; $column <= $column_length; $column++) {
        if ($row == 1 || $row == $row_middle || $row == $row_length) {
            echo "*&nbsp;";
        } else if ($column == 1 && $row < $row_middle) {
            echo "*&nbsp;";
        } else if ($column == $column_length) {
            echo "*&nbsp;";
        } else {
            echo "&nbsp;&nbsp;&nbsp;";
        }
    }
    echo "<br>";
}
?>

PHP program to print number zero with star pattern

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

Program Logic & Code

  • The logic is the same as the program to print hollow square with star pattern.
  • Take 10 rows and 10 columns.
  • On the first and last row, print star in all columns, 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)
  • If the condition does not satisfy, we print spaces.
<?php
$row_length = 10;
$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 == $row_length) {
            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

Leave a Reply

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