A repeated number triangle prints the row digit over and over — row 1 shows 1, row 2 shows 22, row 3 shows 333, and so on.
Remember
Rule: for i = 1..rows
print digit i, exactly i times
1
22
333
4444
55555 ← rows = 5
Unlike Program 5 (prints 1..$i with echo $j), here the inner loop always echoes $i — the counter $j only controls how many copies. Program 10 reverses the outer loop for a descending repeat triangle.
Approach
How to Solve It
Outer loop: $i from 1 to $rows (the digit). Inner loop: echo $i from 1 to $i times. End each row with echo PHP_EOL.
Method
Idea
Best for
Fixed rows
Hard-code height; outer digit, inner count
Labs and demos
CLI input
Same loops; read $rows at runtime
Interactive practice
Spaced digits
echo $i . " " between copies
Easier reading
Pseudocode
Pseudocode
for i from 1 to rows:
for j from 1 to i:
print i
print newline
Cheat sheet
Goal
Pattern
Set size
$rows = 5;
Outer (digit)
for ($i = 1; $i <= $rows; $i++)
Inner (repeat count)
for ($j = 1; $j <= $i; $j++) echo $i;
End the row
echo PHP_EOL;
Shortcut
echo str_repeat((string) $i, $i) . PHP_EOL;
Printing Numbers vs Starting a New Line
API
Effect
Use for
echo $i
Stays on the same line
Each copy of the row digit
echo PHP_EOL
Ends the current line
After the inner loop finishes
Repeat the digit with echo, then break once.
Try it
Live Preview
Change the row count and the repeated number triangle updates instantly.
Whole numbers from 3 to 9. Tap a chip or type a value — the preview redraws as you go.
echo vs PHP_EOL: copies stay on the line; echo PHP_EOL advances after the inner loop.
Next step: Program 10 counts the outer loop down for 5, 44, 333.
One line: on row $i, print digit $i exactly $i times.
Frequently Asked Questions
The outer loop sets $i = 4. The inner loop runs $j from 1 to 4 and each iteration echoes $i, so digit 4 appears four times on that row.
The outer loop runs $i from 1 to $rows. For each $i, the inner loop runs $j from 1 to $i and echoes $i, then echo PHP_EOL ends the row.
Reverse the outer loop: for ($i = $rows; $i >= 1; $i--) and keep the inner loop $j = 1..$i with echo $i. That yields 5, 44, 333, 2222, 11111 for five rows — see Program 10.
Program 5 prints ascending digits 1, 12, 123 (echo $j). Program 9 repeats the row digit: 1, 22, 333 (echo $i). Program 10 is the descending repeat triangle (5, 44, 333, 2222, 11111).
Yes. Use echo $i . " " inside the inner loop — see Example 3.
echo stays on the same line for each digit. echo PHP_EOL ends the row after the inner loop finishes.
O(n²) for n rows. Total printed digits are 1+2+...+n = n(n+1)/2.
Use trim(fgets(STDIN)) and is_numeric($input) before casting to int.
The outer loop never runs, so nothing is printed. Validate and prompt again if you want a clear user message.
🤔
Did you know?
Outer loop sets row digit $i; inner loop prints $i exactly $i times — 1, 22, 333, … O(n²) for $n rows.