PHP Star Pattern 6
Photo Credit to CodeToFun
PHP Star Pattern 6
Here`s a program that prints the above star pattern using PHP Programming:
<?php
for($i=5; $i>=1; $i--)
{
for($j=$i; $j<5; $j++)
echo "&nbsp;&nbsp;";
for($k=$i*2; $k>1; $k--)
echo "*";
echo "<br>";
}
?>
💻 Testing the Program
When you run the above program, it will print the following output:
********* ******* ***** *** *
🧠 How the Program Works
Let's break down the logic behind the code:
- The <?php ... ?> tags indicate the start and end of PHP code.
- for($i=5; $i>=1; $i--): This is the outer loop that controls the number of rows in the pyramid. It starts with $i set to 5 and decrements $i by 1 in each iteration until it becomes 1.
- Inside the outer loop, we have another nested loop: for($j=$i; $j<5; $j++): This loop is responsible for printing the spaces before the stars. It starts with $j set to the current value of $i and increments $j by 1 until it reaches 4 (i.e., $j < 5). This loop prints two non-breaking space characters &nbsp;&nbsp; for each iteration, effectively creating the indentation.
- The next nested loop is: for($k=$i*2; $k>1; $k--): This loop generates the pattern of stars for each row. It starts with $k set to the value of $i multiplied by 2 and decrements $k by 1 in each iteration until it becomes 1. This loop prints '*' characters for each iteration.
- echo "<br>";: After both nested loops complete for a particular row, this line is executed to print a line break (<br>) in HTML, moving to the next line for the next row.
- The outer loop continues until $i becomes 1, generating the entire inverted pyramid pattern.
💯 Tips for Enhancement:
Explore the versatility of this pattern by adjusting its parameters. Whether you increase or decrease the size, tweak the spacing, or modify the characters used, each change opens up a world of possibilities, allowing you to customize and create your unique visual effects.
✔ Conclusion:
Creating visually appealing patterns is not only a fun endeavour but also a great way to enhance your programming or design skills. We hope this tutorial has inspired you to explore the world of creative coding. Share your creations with us, and let your imagination run wild!
🤗 Closing Call-to-Action:
We'd love to see your unique interpretations of the star pattern. Share your creations in the comments below, and don't hesitate to reach out if you have any questions or suggestions for future tutorials. Happy coding!
👨💻 Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (PHP Star Pattern 6) please comment here. I will help you immediately.