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
foror awhileloop. - 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 |
| Interview Questions & Answers |
|---|
| No Content Found. |