Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

PHP Star Pattern 6

Posted in PHP Tutorial
Updated on Jan 10, 2024
By Mari Selvan
👁️ 128 - Views
⏳ 4 mins
💬 1 Comment
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:

example.php
Copied
Copy To Clipboard
<?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:

Output
*********
 *******
  *****
   ***
    *

🧠 How the Program Works

Let's break down the logic behind the code:

  1. The <?php ... ?> tags indicate the start and end of PHP code.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

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

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
1 year ago

If you have any doubts regarding this article (PHP Star Pattern 6) please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy