In this post, we will print the opposite pattern from the pattern printed in the previous post. After reading this post you will be able to make these patterns easily.


How To Print Star Pattern In PHP - 2 ?


Print Star Pattern In PHP

Now we have to print 5 star in the first row. Therefore, we will place the value of $i as 5 and $i-- in the first for loop. Now it will run for loop, then it will run 5 times for the first time and 5 star will be printed in the first row. When the second time runs, the value of $i will be reduced to 4 because of $i-- and then 4 star will be printed in the second row. This way the loop will run until the value of $i is equal to 1.

<?php

for ($i = 5; $i >= 1; $i--) {

for( $j = 1; $j<= $i; $j++) {

echo " * ";

}

echo "<br/>";

}

?> code-box


Print Number Pattern In PHP

There are two types of Number Pattern:

Pattern 1 (1 2 3 4 5 - 1 2 3 4  - 1 2 3)

Replace " * " with $j then we get this pattern;

<?php

for ($i = 5; $i >= 1; $i--) {

for( $j = 1; $j<= $i; $j++) {

echo  $j."  ";

}

echo "<br/>";

}

?> code-box


Pattern 2 (5 5 5 5 5 - 4 4 4 4 - 3 3 3)

Replace $j with $i then we get this pattern:

<?php

for ($i = 5; $i >= 1; $i--) {

for( $j = 1; $j<= $i; $j++) {

echo  $i."  ";

}

echo "<br/>";

}

?> code-box


Summary

In this post we learn how to print opposite star pattern from the pattern printed in the previous post in PHP. You try to make it yourself. I hope you make. If you face any problem, then comment and tell us. Keep visiting the blog to read more similar posts.

Post a Comment

Previous Post Next Post