PHP Ascending Number Triangle Pattern

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

What Is This Pattern?

An ascending number triangle grows one digit per row: row $i prints 1 through $i with no spaces between digits. With $rows = 5 you get 1, 12, 123, 1234, 12345.

Remember
Rule: on row i, print j for j = 1..i

1
12
123
1234
12345     ← rows = 5

Unlike Program 4 (descending $rows..$i shrink), this shape counts the outer loop up so each row is longer than the last.

How to Solve It

Outer loop picks row $i from 1 to $rows. Inner loop prints digits 1..$i.

MethodIdeaBest for
Tight digitsecho $j with no spacesClassic textbook demos
Spaced digitsecho $j . " "Easier reading when digits get wider

Pseudocode

Pseudocode
for i from 1 to rows:
    for j from 1 to i:
        print j (no newline)
    print newline

Cheat sheet

GoalPattern
Outer rowsfor ($i = 1; $i <= $rows; $i++)
Inner digitsfor ($j = 1; $j <= $i; $j++)
Print digitecho $j;
Spaced formecho $j . " ";
End the rowecho PHP_EOL;

Printing Numbers vs Starting a New Line

APIEffectUse for
echo $jStays on the same lineEach digit
echo PHP_EOLEnds the current lineAfter the inner loop

Print digits without a newline, then end the row once. Putting echo PHP_EOL inside the inner loop turns the triangle into a vertical list.

Live Preview

Change the row count and the ascending triangle updates instantly.

Use rows from 1 to 9 (single digits stay tidy). Tap a chip or type a value — the preview redraws as you go.

Live result 5 rows · 15 digits
1
12
123
1234
12345

Worked Walkthrough

Trace row $i = 4 — four digits glued together as 1234.

$jPrintLine so far
111
2212
33123
441234

After $j finishes, echo PHP_EOL starts the next row. Row 5 will print one more digit — 12345.

PHP Programs

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

Example 1 — Fixed $rows = 5

Hard-coded height — outer loop grows the row, inner loop prints 1..$i.

PHP
<?php
$rows = 5;

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

How It Works

1. Outer loop grows the row. $i runs from 1 to 5 — one more digit each line.

2. Inner loop prints 1..$i. $j runs from 1 to $i; each digit uses echo $j with no space.

3. Newline after the row. echo PHP_EOL runs only after the inner loop finishes.

Example 2 — User Input Rows

Read the row count at runtime so the triangle grows or shrinks with input.

PHP
<?php
echo "Enter the number of rows: ";
$input = trim(fgets(STDIN));

if (!is_numeric($input) || (int) $input <= 0) {
    echo "Please enter a positive integer." . PHP_EOL;
    exit(1);
}

$rows = (int) $input;

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

How It Works

1. Same nested-loop core. Only the source of $rows changes from a literal to CLI input.

2. Entering 4 stops early. You get four rows ending at 1234.

3. Safer input tip. Cap demos for readable output:

Safer input tip
if (!is_numeric($input) || (int) $input < 1 || (int) $input > 9) {
    echo "Enter a whole number from 1 to 9." . PHP_EOL;
    exit(1);
}

Example 3 — Spaced Digits

Same loops — add a space after each digit for easier reading.

PHP
<?php
$rows = 5;

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

How It Works

1. Only the print changes. echo $j . " " instead of echo $j.

2. Loop bounds stay the same. Outer and inner limits match Example 1 exactly.

3. Useful for wider values. Spaces keep columns readable when digits become two characters.

Edge Cases & Pitfalls

Check these before calling the solution done.

Newline

Do not put PHP_EOL inside the inner loop

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

Outer direction

Count up, not down

Outer $i = $rows..1 with the same inner loop is Program 1 (shrinking). This pattern uses $i = 1..$rows.

Missing newline

Omitting the row break

Without echo PHP_EOL, every digit glues onto one endless line.

j > i

Wrong inner bound

Using $j <= $rows makes every row the same length. Keep $j <= $i.

rows = 1

Single value

Output is just 1. A good sanity check for input validation.

fgets

Check is_numeric

Bare (int) casts turn non-numeric strings into 0 — prefer is_numeric before casting.

Time and Space Complexity

ProgramTimeExtra space
Nested loops (Examples 1–3)O(n²)O(1)

Total prints are 1 + 2 + … + n = n(n + 1) / 2 — quadratic in n. Only a few integers of extra memory.

Key Takeaways

  • Rule: on row $i, print $j for $j = 1..$i.
  • Grow up: outer $i = 1..$rows makes each row one digit longer.
  • Break the row: call echo PHP_EOL only after the inner loop.
  • Complexity: O(n²) time; O(1) extra space.

One line: for each row $i from 1 to $rows, print digits 1..$i, then end the line.

Frequently Asked Questions

The outer loop runs $i from 1 to $rows. For each row $i, the inner loop runs $j from 1 to $i and prints $j. Row 1 prints 1, row 2 prints 12, and so on until row $rows prints 1..$rows.
Because the outer loop counts up and the inner bound equals $i. When $i increases, each row prints one more digit than the previous row.
Program 4 prints $rows..$i in descending order (54321, 5432, …). Program 5 prints 1..$i in ascending order — a growing triangle instead of a shrinking one.
Program 1 counts the outer loop down and prints 12345, 1234, … Program 5 counts up and prints 1, 12, 123, … — same inner loop, opposite outer direction.
Printing a digit stays on the same line. Printing PHP_EOL ends the current row. Digits use echo without a newline; the row break uses echo PHP_EOL after the inner loop.
O(n²) for n rows because total prints are 1 + 2 + … + n = n(n+1)/2.
Use echo $j . " " instead of echo $j — see Example 3.
Use trim(fgets(STDIN)) and check is_numeric($input) before casting to int — see Example 2.

Did you know?

Row $i prints digits 1 through $i. The outer loop counts up from 1 to $rows, so each row grows by one digit — total prints = n(n+1)/2, time O(n²).

Next: Increasing Suffix Pattern

Continue with the next pattern in the PHP number-pattern series.

Program 6 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