PHP Number Triangle Pattern (Repeated Digits)

Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

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.

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.

MethodIdeaBest for
Fixed rowsHard-code height; outer digit, inner countLabs and demos
CLI inputSame loops; read $rows at runtimeInteractive practice
Spaced digitsecho $i . " " between copiesEasier reading

Pseudocode

Pseudocode
for i from 1 to rows:
    for j from 1 to i:
        print i
    print newline

Cheat sheet

GoalPattern
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 rowecho PHP_EOL;
Shortcutecho str_repeat((string) $i, $i) . PHP_EOL;

Printing Numbers vs Starting a New Line

APIEffectUse for
echo $iStays on the same lineEach copy of the row digit
echo PHP_EOLEnds the current lineAfter the inner loop finishes

Repeat the digit with echo, then break once.

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.

Live result rows = 5 · digits = 15
1
22
333
4444
55555

Worked Walkthrough

Trace $rows = 5, row $i = 3 — the inner loop builds 333.

StepLoopResult
Outer$i = 3Digit to print = 3
Inner$j = 1..3, each echoes $i333
Breakecho PHP_EOLNext row starts

Key detail: print $i, not $j. Echoing $j would build Program 5 (123) instead of 333.

PHP Programs

Three complete programs: fixed $rows = 5, CLI input, and spaced digits. Use View Output for sample results.

Example 1 — Fixed $rows = 5

Hard-coded height — outer $i is the digit; inner $j repeats it $i times.

PHP
<?php
$rows = 5;

for ($i = 1; $i <= $rows; $i++) {
    for ($j = 1; $j <= $i; $j++) {
        echo $i;
    }
    echo PHP_EOL;
}
?>

How It Works

1. Choose the digit. Outer $i is both the row number and the value to print.

2. Repeat it. Inner loop runs $i times and always echoes $i (not $j).

3. Row break. echo PHP_EOL after the inner loop starts the next line.

Example 2 — User Input

Read $rows from the CLI. Prefer is_numeric (shown below).

PHP
<?php
echo "Enter the number of rows: ";
$input = trim(fgets(STDIN));
if (!is_numeric($input) || (int) $input < 1) {
    echo "Enter a positive whole number." . PHP_EOL;
    exit(1);
}
$rows = (int) $input;

for ($i = 1; $i <= $rows; $i++) {
    for ($j = 1; $j <= $i; $j++) {
        echo $i;
    }
    echo PHP_EOL;
}
?>

How It Works

1. Validate input. is_numeric rejects letters; require a positive whole number.

2. Same loops. Outer digit and inner repeat match Example 1 — only the source of $rows changes.

3. Scale. For $rows = 4, the last line is 4444.

Example 3 — Spaced Digits

Same logic; print a space after each repeated digit for easier reading.

PHP
<?php
$rows = 5;

for ($i = 1; $i <= $rows; $i++) {
    for ($j = 1; $j <= $i; $j++) {
        echo $i . " ";
    }
    echo PHP_EOL;
}
?>

How It Works

1. Same shape. Outer and inner bounds are unchanged; still echo $i.

2. Spacing. echo $i . " " separates copies without changing the count.

3. Still one break. echo PHP_EOL after the inner loop ends the row.

Edge Cases & Pitfalls

Check these before calling the solution done.

echo $j

Printing $j instead of $i

That builds Program 5 (1, 12, 123). Keep echo $i so the row digit repeats.

wrong count

Inner loop bound not equal to $i

Use for ($j = 1; $j <= $i; $j++) so row length matches the digit.

PHP_EOL inside

echo PHP_EOL inside the inner loop

That prints one digit per line. Call echo PHP_EOL only after the inner loop finishes.

rows = 1

Single digit

Output is one line: 1.

rows ≤ 0

Empty output

The outer loop never runs. Validate and prompt again for clearer UX.

multi-digit

$rows > 9

echo 10 prints two characters per “digit” and widens the row. Labs usually stick to 1–9.

Time and Space Complexity

ProgramTimeExtra space
Fixed / input / spaced (Examples 1–3)O(n²)O(1)

There are n rows, and total digits printed are 1 + 2 + … + n = n(n+1)/2, so work is O(n²).

Key Takeaways

  • Rule: outer picks digit $i; inner prints $i exactly $i times.
  • Echo $i, not $j: $j is only the repeat counter.
  • 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.

Next: Descending Repeating Triangle

Continue with rows like 5, 44, 333.

Program 10 tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful