PHP Continuous Number Triangle Pattern

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

What Is This Pattern?

A continuous number triangle grows one number longer each row, but the counter never resets — values keep climbing across the whole pattern.

Remember
Rule: k = 1; for i = 1..n, print k++ (i times)

1
2 3
4 5 6
7 8 9 10   ← 4 rows

In PHP declare $k = 1 once, print $k++ in the inner loop for $i iterations per row, then echo PHP_EOL ends the row. Program 21 jumps ahead with a different step rule.

How to Solve It

Declare $k = 1 before both loops. Outer loop from 1 to $rows. Inner loop prints $k++ exactly $i times. End each row with echo PHP_EOL.

MethodIdeaBest for
Running counter$k outside loops; print $k++ each cellLearning, interviews, exams
Custom startSet $k to any start value before the loopsWhen the sequence should not begin at 1

Pseudocode

Pseudocode
k = 1
for i from 1 to rows:
    for j from 1 to i:
        print k and a space
        k = k + 1
    print newline

Cheat sheet

GoalPattern
Init counter$k = 1; before both loops
Pick row lengthfor ($i = 1; $i <= $rows; $i++)
Print & advanceecho $k++ . " ";
End the rowecho PHP_EOL;
Custom start$k = 10; (or any value) before the loops

Printing Numbers vs Starting a New Line

APIEffectUse for
echo $k++ . " "Stays on the same lineEach number in the row
echo PHP_EOLEnds the current lineAfter the inner loop

Print all numbers without a newline, then end the row once.

Live Preview

Change the row count and the continuous triangle updates instantly.

Whole numbers from 1 to 12. Tap a chip or type a value — the preview redraws as you go.

Live result 4 rows · 10 numbers
1
2 3
4 5 6
7 8 9 10

Worked Walkthrough

Trace $rows = 4 with $k starting at 1 — watch the counter carry into the next row.

$iPrints$k after rowPrinted row
11×21
22×42 3
33×74 5 6
44×117 8 9 10

Total numbers are 1+2+3+4 = 10 — the triangular number n(n+1)/2.

PHP Programs

Three complete programs: fixed height, CLI input, and a custom start for $k. Use View Output for sample results.

Example 1 — Fixed $rows = 4

Declare $k once; print and advance it in the inner loop.

PHP
<?php
$rows = 4;
$k = 1;

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

How It Works

1. Counter lives outside. $k = 1 is set once so its value survives every row.

2. Row length grows. Outer $i is both the row index and how many numbers that row prints.

3. Print then increment. echo $k++ . " " prints the current value, then advances $k for the next cell.

Example 2 — User Input

Read the row count from the CLI. Prefer is_numeric in real apps (shown in the tip below).

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

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

How It Works

1. Read rows. Ask for a row count, then trim(fgets(STDIN)) and cast to int.

2. Same nested loops. Three rows print 1, 2 3, 4 5 6.

3. Safer input tip. Bare (int) casts turn letters into 0. Prefer:

Safer input
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;

Example 3 — Custom Start Value

Start $k at 10 — same triangle shape, different sequence.

PHP
<?php
$rows = 4;
$k = 10;

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

How It Works

1. Same structure. Outer and inner loops match Example 1.

2. Different seed. $k = 10 only changes where the sequence begins.

3. Same length rule. Rows still print 1, 2, 3, then 4 numbers — only the values shift.

Edge Cases & Pitfalls

Check these before calling the solution done.

reset k

$k = 1 inside the outer loop

That restarts every row at 1. Keep $k outside both loops for a continuous sequence.

PHP_EOL inside

echo PHP_EOL inside the inner loop

That puts each number on its own line. Call echo PHP_EOL only after the row finishes.

rows = 1

Single number

Output is just 1 — a good sanity check.

rows ≤ 0

Empty output

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

multi-digit

Large row counts

After 9 the counter becomes two digits. Spacing still works; alignment just looks wider.

spaces

Keep the trailing space

The classic sample uses echo $k++ . " " so numbers stay spaced and readable.

Time and Space Complexity

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

There are n rows printing 1+2+…+n numbers, so total work is n(n+1)/2.

Key Takeaways

  • Rule: init $k = 1 once; for each row $i, print $k++ exactly $i times.
  • Scope: keep $k outside the outer loop so the sequence never restarts.
  • echo vs PHP_EOL: numbers stay on the line; echo PHP_EOL advances after each row.
  • Next step: Program 21 jumps ahead with a different increment rule.

One line: keep $k outside, print $k++ $i times per row, then echo PHP_EOL.

Frequently Asked Questions

Because $k is declared once ($k = 1) before the loops and incremented with $k++ every time a number is printed — its value carries over to the next row.
Row 1 prints 1, so $k becomes 2. Row 2 prints $k twice: 2, then 3 after $k++.
$k resets each row and the sequence restarts at 1 — you get a growing triangle, not a continuous counter.
echo $k++ . " " stays on the same line with a trailing space. echo PHP_EOL ends the current line. Numbers use echo; the row break uses echo PHP_EOL after the inner loop.
Total prints are 1+2+...+n = n(n+1)/2.
Yes. Set $k = 10 (or any value) before the loops — the sequence continues from that start (see Example 3).
O(n²) for n rows because total digit prints equal n(n+1)/2.
Use trim(fgets(STDIN)) and check is_numeric($input) before casting to int so bad input does not produce unexpected results.
The outer loop never runs, so nothing is printed. Validate and prompt again if you want a clear user message.

Did you know?

A single counter k starts at 1 and increments with k++ on every print — numbers continue across rows instead of restarting. Total prints still equal n(n+1)/2 for n rows.

Next: Jump Number Triangle

Continue with a counter that jumps ahead each row.

Program 21 tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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