Write a Program to create given pattern with * using for loop

Description: Write a PHP program to print this pattern using loops:

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

Rules and hints:

  • You can use a for or a while loop.
  • Use a nested loop (a loop inside a loop).

PHP program to print the star pattern using two for loops

<?php
for ($row = 1; $row <= 8; $row++) {
    for ($star = 1; $star <= $row; $star++) {
        echo "*";
    }
    echo "<br>";
}
?>

Output:

Tutorials Class - Output Window

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

How it works

  • The outer loop makes 8 rows.
  • The inner loop prints as many stars as the number of the current row (1 star in row 1, 2 stars in row 2, and so on).
  • After every row, <br> moves to the next line.

Shorter version

<?php
for ($row = 1; $row <= 8; $row++) {
    echo str_repeat("*", $row) . "<br>";
}
?>

Learn more about the similar topics:
Tutorials
PHP Loops
Exercises & Assignments
Write a program to count 5 to 15 using PHP loop
Write a factorial program using for loop in php
Factorial program in PHP using recursive function
Write a program to create Chess board in PHP using for loop
Write a Program to create given pattern with * using for loop
Interview Questions & Answers
No Content Found.