PHP Star Pyramid Patterns Programs Simple and Easy Way to Print

php star pattern program with example; In this tutorial we learn PHP Star Pyramid Patterns programs with full code to print different types of tringles to pring. We have added various types of star (*) patterns with best one loop, two loop example to print different types of star pattern in PHP easy and simple way.

These all the star pattern examples are useful for interview questions of PHP. Interviewers may ask questions based on creating star patterns in PHP. You can implement and see the output all the star pyramid patterns easy way in php.

Print Star Pattern in PHP example are created in one or two loops, not extrap for loops, without any extra loops. This tutorial will guide you about the star print program in PHP and all the different methods and the various patterns that are involved in this subject.

Example 1: Simple Pyramid Pattern

Below example show the simple start print print pyramid, you can print this using one loop also, we have added the example 7:

<?php
for ($i=1; $i <5 ; $i++) {
    for ($j=1; $j <=$i ; $j++) {
        echo "*";
    }
    echo "<br>";
}
?>

Output:

*
**
***
****

Example 2: Easy 180 Degree Rotation Star Print

If you want to create 180 degrees rotated simple pyramid pattern then below example is perfect for you.

<?php
for ($i=1; $i <5 ; $i++) {
    for ($j=$i; $j <5 ; $j++) {
        echo "*";
    }
    echo "<br>";
}
?>

Output:

****
***
**
*

Example 3: After 180 degree rotation Pyramid

Here is the another start pyramid example which is asked in php interviews.

<?php
    for ($i=4; $i >=1 ; $i--) {
        for ($j=1; $j <=4 ; $j++) {
            if($j>=$i) {
                echo "*";
            } else {
                echo "&nbsp;";
            }
        }
        echo "<br>";
    }
?>

Output:

   *
  **
 ***
****

Example 4: Simle start print only Last time start

That’s below example which is the best way to show only last time star (*);

<?php
    for ($i=1; $i < 5; $i++) {
        for ($j=1; $j < 5 ; $j++) {
           if ($j == $i) {
               echo "*";
           } else {
               echo "&nbsp;";
           }
        }
        echo "<br>";
    }
?>

Output:

*
 *
  *
   *

Example 5: Single Start (*) print in Last

<?php
 for ($i=1; $i < 5; $i++) {
     for ($j=$i; $j <=5 ; $j++) {
        if ($j == 5) {
            echo "*";
        } else {
            echo "&nbsp;";
        }
     }
     echo "<br>";
 }
?>

Output:

   *
  *
 *
*

Example 6: Triangle Pyramid Star Pattern in PHP

<?php
    for ($i=0; $i <4 ; $i++) {
        for ($j=1; $j <4-$i; $j++) {
            echo "&nbsp;&nbsp;";
        }

        for ($k=1; $k <= (2*$i+1); $k++) {
            echo "*";
        }

        echo "<br>";
    }

 ?>

Output:

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

Example 7: Simple Star Print in One Loop

Here the simple star print using one loop.

<?php
    $x = '';
    for ($i=0; $i <4 ; $i++) {
        $x .= "*";
        echo $x;
        echo "<br>";
    }

 ?>

Output:

*
**
***
****

I hope you loved this article. Please share this with your friends and if you have any other tips or star pyramid comment us below.

Leave a Comment