PHP Number Triangle Pattern (Starting from 11)

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

What Is This Pattern?

An increasing number triangle from 11 prints $i values on row $i, each computed as 9 + $i + $j — so the first cell is always 11.

Remember
Rule: for i from 1 to rows
        for j from 1 to i
          print (9 + i + j) and a space

11
12 13
13 14 15
14 15 16 17
15 16 17 18 19     ← rows = 5

Follows the number-star diamond in Program 31; next is the same shape starting from 1 in Program 33.

How to Solve It

Outer loop grows $i. Inner loop prints $i values with the formula 9 + $i + $j, separated by spaces.

MethodIdeaBest for
Formula 9 + $i + $jBase offset 9 shifts the triangle to start at 11Learning, interviews, exams
Custom base$baseVal + $i + $j with CLI inputFlexible starting number

Pseudocode

Pseudocode
for i from 1 to rows:
    for j from 1 to i:
        print (9 + i + j) and a space
    print newline

Cheat sheet

GoalPattern
Grow each rowfor ($i = 1; $i <= $rows; $i++)
Print i valuesfor ($j = 1; $j <= $i; $j++)
Start at 11echo (9 + $i + $j) . " ";
Custom baseecho ($baseVal + $i + $j) . " ";
End of rowecho PHP_EOL;

Printing Numbers vs Starting a New Line

APIEffectUse for
echo value . " "Stays on the same lineEach number on the row
echo PHP_EOLEnds the current lineAfter the inner loop finishes

Print values without a newline, then end the row once.

Live Preview

Change the row count and the increasing triangle updates instantly — capped at 9 for readable demos.

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

Live result rows = 5 · 15 numbers
11 
12 13 
13 14 15 
14 15 16 17 
15 16 17 18 19 

Worked Walkthrough — $rows = 5

Trace how each ($i, $j) pair maps to 9 + $i + $j.

$iValuesCountPrints
111111
212, 13212 13
313, 14, 15313 14 15
414..17414 15 16 17
515..19515 16 17 18 19

Total numbers = 1 + 2 + … + n = n(n+1)/2 → O(n²).

PHP Programs

Three complete programs: fixed $rows = 5, custom base with CLI input, and a compact $rows = 3 demo. Use View Output to reveal sample results.

Example 1 — Fixed $rows = 5

Hard-coded height — print 9 + $i + $j with a trailing space on each cell.

PHP
<?php
for ($i = 1; $i <= 5; $i++) {
    for ($j = 1; $j <= $i; $j++) {
        echo (9 + $i + $j) . " ";
    }
    echo PHP_EOL;
}
?>

How It Works

1. Outer grows the width. Row $i prints exactly $i numbers.

2. Formula fills the cells. 9 + $i + $j makes the first cell 11 and each row start one higher.

3. Newline once. Call echo PHP_EOL only after the inner loop finishes.

Example 2 — Custom Base and Rows

Read $rows and $baseVal from the CLI, validate, then use $baseVal + $i + $j.

PHP
<?php
echo "Enter rows: ";
$rowsInput = trim(fgets(STDIN));

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

echo "Enter base: ";
$baseInput = trim(fgets(STDIN));

if (!is_numeric($baseInput)) {
    echo "Please enter a whole number." . PHP_EOL;
    exit(1);
}

$rows = (int) $rowsInput;
$baseVal = (int) $baseInput;

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

How It Works

1. Prompt and validate. Reject non-numeric input and non-positive $rows before printing.

2. Same core. $baseVal replaces hard-coded 9 — use 9 to start at 11.

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

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

Example 3 — Compact $rows = 3

Same structure with only three rows — easy to confirm the formula on paper.

PHP
<?php
$rows = 3;

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

How It Works

1. Three rows. $i = 1 → 11; $i = 2 → 12 13; $i = 3 → 13 14 15.

2. Trace on paper. If you forget the trailing space, numbers glue together (1213).

3. Scale up next. Once the small demo is clear, use Examples 1–2 for five rows or a custom base.

Edge Cases & Pitfalls

Check these before calling the solution done.

wrong base

Offset off by one

Using 10 + $i + $j or $i + $j shifts every value — the triangle no longer starts at 11. Keep 9 + $i + $j.

j <= rows

Rectangle instead of triangle

Inner bound $j <= $rows makes every row the same width. Use $j <= $i.

no space

Glued numbers

Without . " ", values run together (1213). Always print a trailing space.

PHP_EOL inside

Broken rows

If echo PHP_EOL sits inside the inner loop, you get one number per line. Call it only after the loop.

rows = 1

Single value

Output is just 11. 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
Fixed / input (Examples 1–2)O(n²)O(1)
Compact rows = 3 (Example 3)O(n²)O(1)

Row $i prints i numbers. Total = 1 + 2 + … + n = n(n+1)/2 → O(n²) time. Only a few loop variables are needed.

Key Takeaways

  • Formula: each cell is 9 + $i + $j — first cell is always 11.
  • Growing width: inner loop runs $j = 1..$i so row $i has $i numbers.
  • Break the row: values with spaces in the inner loop; echo PHP_EOL after it finishes.
  • Complexity: O(n²) time from n(n+1)/2 prints; O(1) extra space.

One line: for each row $i, print 9 + $i + $j for $j = 1..$i with spaces, then echo PHP_EOL.

Frequently Asked Questions

An increasing triangle from 11: for rows=5 you get 11 / 12 13 / 13 14 15 / 14 15 16 17 / 15 16 17 18 19 — each value is 9 + $i + $j.
Because the printed value is 9 + $i + $j. On the first row $i = 1 and $j = 1, so 9 + 1 + 1 = 11.
It is a base offset. Change 9 to any base value to shift the entire triangle — see Example 2.
Program 32 uses 9 + $i + $j (starts at 11). Program 33 uses $i + $j - 1 (starts at 1).
echo (9 + $i + $j) . " " keeps values separated on the same row. echo PHP_EOL ends the row.
echo value . " " stays on the same line. echo PHP_EOL ends the row after the inner loop finishes.
Use trim(fgets(STDIN)) and check is_numeric($input) before casting to int — see Example 2.
O(n²) for n rows because total prints are 1 + 2 + … + n = n(n+1)/2.

Did you know?

Each printed value is computed as 9 + $i + $j. Row $i = 1 prints 11; row $i = 2 prints 12 and 13 — a left-shifted increasing triangle.

Next: Increasing Triangle from 1

Continue with the same shape using $i + $j - 1 instead of 9 + $i + $j.

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