Today, We want to share with you star pattern in php.In this post we will show you number pattern programs in php, hear for PHP programs for printing pyramid patterns we will give you demo and example for implement.In this post, we will learn about PHP Nested Forloop – Exercises Pyramid Program Solution with an example.
All star pattern programs in PHP
Number pattern stars in PHP can be printed using for and foreach loop. There are a some of patterns in stars pattern. Some of them are display here.
you will learn about the process to print star pattern in PHP.
Print Triangle of Stars in PHP
<?php for($no=0;$no<=6;$no++) { for($k=6;$k>=$no;$k--) { echo " "; } for($x=1;$x<=$no;$x++) { echo "* "; } echo "<br>"; } for($no=5;$no>=1;$no--) { for($k=6;$k>=$no;$k--) { echo " "; } for($x=1;$x<=$no;$x++) { echo "* "; } echo "<br>"; } ?>
Results:
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Example 2: Print Star Pattern in PHP
<?php for ($xdk = 1; $xdk <= 5; $xdk++) { for ($col = 1; $col <= ($xdk > 3 ? 6 - $xdk : $xdk); $col++) { echo '* '; } echo "</br>"; } ?>
Results:
* * * * * * * * *
Example 3: Print Triangle of Stars in PHP
<?php for($a=5; $a>=1; $a--) { if($a%2 != 0) { for($b=5; $b>=$a; $b--) { echo "* "; } echo "<br>"; } } for($a=2; $a<=5; $a++) { if($a%2 != 0) { for($b=5; $b>=$a; $b--) { echo "* "; } echo "<br>"; } } ?>
Results:
* * * * * * * * * * * * *
Example 4: Print Star Pattern in PHP
<?php for($no=0;$no<=5;$no++) { for($x=5-$no;$x>=1;$x--) { echo "* "; } echo "<br>"; } ?>
Results:
* * * * * * * * * * * * * * * * * * * * * * * * * * * *
Example 5: Print Star Pattern in PHP
<?php for($no=0;$no<=5;$no++) { for($x=0;$x<=$no;$x++) { echo "* "; } echo "<br>"; } ?>
Results:
* * * * * * * * * * * * * * * * * * * * * * * * * * * *
Example 6: Print Triangle of Stars in PHP
<?php for ($xdk = 1; $xdk <= 5; $xdk++) { for ($col = 1; $col <= 5; $col++) { echo '* '; } echo "</br>"; } ?>
Results:
* * * * * * * * * * * * * * * * * * * * * * * * *
Example 7:
<?php for ($xdk = 1; $xdk <= 5; $xdk++) { echo "* </br>"; } ?>
Results:
* * * * *
I hope you get an idea about PHP for loop – Exercises, Practice, Solution.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.