Increasing Number Triangle Starting from 11 in PHP

Beginner
⏱️ 9 min read
📚 Updated: Aug 2026
🎯 3 Code Examples
🚀 Live Preview
Formula + Loops

What You’ll Learn

The increasing number triangle from 11 prints 11, 12 13, 13 14 15, … — a natural step after the number-star diamond in Program 31. This tutorial covers the 9 + $i + $j formula, nested loops, a live preview, worked PHP examples, edge cases, and complexity.

Shape Rule

Left-shifted triangle

Row $i prints $i numbers computed as 9 + $i + $j.

Outer Loop

$i = 1..$rows

for ($i = 1; $i <= $rows; $i++) — one growing row per iteration.

Inner Loop (j)

1..i

for ($j = 1; $j <= $i; $j++) — prints $i values per row.

Formula

9 + $i + $j

Base offset 9 shifts the triangle to start at 11.

Live Preview

3–9 rows

Pick a row count and draw the increasing triangle in the browser.

O(n²)

Complexity

Prints per row = $i — total work scales as .

Introduction

A left-shifted increasing number triangle prints values from the formula 9 + $i + $j on each row. With $rows = 5, you get 11, 12 13, 13 14 15, and so on.

In PHP you use nested loops: outer $i = 1..$rows, inner $j = 1..$i, printing (9 + $i + $j) with a trailing space.

Why it matters?

It combines nested loops with an arithmetic formula — a step up from Program 31’s modulus diamond.

Key Highlights

9 + $i + $j

Formula for each value.

Inner $j <= $i

Growing row width.

Starts at 11

When base = 9, i=1, j=1.

Series Foundation

Follow Program 31; continue to Program 33 ($i + $j - 1) next.

In short: outer loop $i = 1..$rows, inner $j = 1..$i, print 9 + $i + $j with a space, then echo PHP_EOL.

📝 Problem & Approach

Given $rows = 5, print a left-shifted increasing triangle: for each row $i, print $j = 1..$i values of 9 + $i + $j separated by spaces.

PHP
// $rows = 5 (conceptual shape)
// 11
// 12 13
// 13 14 15
// 14 15 16 17
// 15 16 17 18 19

Inputs & Outputs

ItemTypeDescription
$rowsintTriangle height — number of lines to print.
$iintOuter loop — current row; also part of the formula.
$jintInner loop — column index; runs 1..i per row.
baseintOffset in the formula (default 9); first value = base + 2.

Minimal workflow

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

Approach comparison

ApproachIdeaBest for
Fixed formula11, 12 13, …Learning and interviews
Custom base($baseVal + $i + $j)Flexible starting number
User-input rows(int) trim(fgets(STDIN));Configurable triangle size

⚡ Quick Reference

GoalPattern
Outer loopfor ($i = 1; $i <= $rows; $i++)
Inner loopfor ($j = 1; $j <= $i; $j++)
Print valueecho (9 + $i + $j) . " ";
End the rowecho PHP_EOL;
Custom baseecho ($baseVal + $i + $j) . " ";
User input(int) trim(fgets(STDIN));

📋 Fixed vs User Input vs Compact Demo

Same increasing triangle — different ways to control rows and the base offset.

Outer loop
$i = 1..$rows

One growing row per iteration

Formula
9 + $i + $j

Starts at 11

Inner loop
$j = 1..$i

i values per row

Learning tip
base + 2

First printed number

Context

When This Pattern Shows Up

Reach for this pattern when teaching formula-based output, growing inner loops, and arithmetic in nested loops.

  1. Post diamond exercise

    Natural follow-up after Program 31 — introduces an arithmetic formula instead of modulus.

  2. Nested-loop warm-up

    Outer/inner bound practice with an immediate visual check.

  3. Console I/O practice

    Combine loops with fgets(STDIN) for a flexible row count.

  4. Gateway to variants

    Compare Program 31 (number-star diamond) and Program 33 ($i + $j - 1) next.

  5. Not a UI layout tool

    This is a console teaching pattern — not how you build modern app screens.

Key benefit: one small program that locks in nested loops, output sequencing, and O(n²) thinking.

🔮 Live Preview

Choose a row count between 3 and 9 and draw the increasing triangle in the browser.

Try 4, 5, or 7. Max up to 9 in this preview.

Live result
Press "Draw pattern".

Examples Gallery

Three complete PHP programs — fixed rows, custom base input, and a smaller trace demo. Click View Output to reveal sample console results.

📚 Getting Started

Print five rows of the increasing triangle with the 9 + $i + $j formula.

Example 1 — Fixed $rows = 5

Hard-coded row count — ideal for first demos and screenshots.

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

How It Works

When $i = 1, the inner loop prints 9+1+1 = 11. When $i = 3, it prints 13, 14, 15 — output 13 14 15.

📈 User Input

Read row count and base offset with fgets(STDIN) instead of hard-coding 5 and 9.

Example 2 — Custom base and rows

Read $rows and $baseVal with fgets(STDIN) instead of hard-coding 5 and 9.

PHP
<?php
echo "Enter rows: ";
$rows = (int) trim(fgets(STDIN));
echo "Enter base: ";
$baseVal = (int) trim(fgets(STDIN));
if ($rows < 1) return;

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

How It Works

Same formula core as Example 1; $baseVal replaces hard-coded 9 and $rows replaces 5. Non-numeric input leaves variables unset if you skip is_numeric() checks — always check it in safer labs.

⚡ Smaller Demo

Run with $rows = 3 to trace every row on paper before scaling up.

Example 3 — Compact $rows = 3

Same nested-loop formula with a smaller row count for quick tracing.

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

Only $rows changes from 5 to 3 — the nested-loop formula stays identical. Trace $i = 1, 2, 3 on paper to see how each row adds one more value.

🧠 How the Algorithm Prints Rows

1

Set up

Set $rows = 5; and use fgets(STDIN) when reading input. Set loop variables $i, $j.

Setup
2

Outer loop walks rows

for ($i = 1; $i <= $rows; $i++) — one growing row per iteration.

Row
3

Inner loop (j)

for ($j = 1; $j <= $i; $j++) — prints $i values per row.

Grow
4

Print formula

echo (9 + $i + $j) . " " — each value from the arithmetic formula.

Formula
5

New line

echo PHP_EOL ends the row after the inner loop finishes.

Break
=

Increasing triangle complete

Prints per row = $iO(n²) time, O(1) extra memory.

🔎 Worked Walkthrough — $rows = 5

Trace each outer-loop value of $i, inner-loop range, values printed, and full row output.

$iInner range ($j)Values (9+i+j)Row output
111111
21, 212, 1312 13
31, 2, 313, 14, 1513 14 15
41..414, 15, 16, 1714 15 16 17
51..515, 16, 17, 18, 1915 16 17 18 19

Prints per row = $i — total prints = n(n+1)/2 for n rows.

Use Cases

Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.

1. Teaching Nested Loops

Clearest visual proof that outer and inner bounds interact.

Example: change inner bound to $j <= $rows and watch every row print the same width.

2. Pattern Series Base

Foundation for formula-based triangles, custom bases, and left-shifted sequences.

Example: continue to Program 33 for the $i + $j - 1 variant starting at 1.

3. Console Formatting Drills

Practice echo.print vs row newline without complex math.

Example: put echo PHP_EOL inside the inner loop by mistake.

4. Padding character

Add spaces between digits once the two-loop structure works.

Example: use echo j) between digits for wider spacing.

5. Complexity Intuition

Triangular totals make O(n²) concrete for beginners.

Example: count printed numbers for $rows = 5 — total is 1+2+3+4+5 = 15.

6. Input Validation Labs

Pair the pattern with is_numeric(trim($line)) checks and positive-row checks.

Example: reject max <= 0 and re-prompt.

Pro Tip: when an interviewer asks for patterns, explain the outer/inner roles first — then write the loops. The story matters as much as the code.

Advantages

Why this pattern earns a permanent spot in beginner PHP courses.

  1. 1. Instant Visual Feedback

    Wrong bounds show up immediately as a broken staircase.

  2. 2. Minimal Concepts

    Only loops and console output — no arrays or math libraries.

  3. 3. Easy to Extend

    Invert, center, hollow, or change the fill character with small edits.

  4. 4. O(1) Extra Memory

    Streaming output needs no storage beyond loop counters.

Pro Tip: trace $i and $j on paper for $rows = 3 before coding — watch how each row prints $i values starting at 9 + $i + 1.

Usage Tips

Small habits that keep number-pattern code clean.

  1. 1. Growing Inner Loop

    Inner bound must be $j <= $i — row $i prints exactly $i numbers.

  2. 2. Prefer trim(fgets(STDIN))

    Call is_numeric(trim($line)) so bad input does not leave variables uninitialized.

  3. 3. Keep echo PHP_EOL Outside

    Only call echo PHP_EOL after the inner loop finishes the row.

  4. 4. Trace 9 + $i + $j on Paper

    Trace the formula for each ($i, $j) pair before coding the loops.

  5. 5. Dry-Run rows = 3

    Trace $i = 1..3 on paper before coding the full $rows = 5 demo.

Pro Tip: if the output is a vertical list of single digits per line, you almost certainly put echo PHP_EOL inside the inner loop.

Common Pitfalls

Mistakes that commonly break increasing number triangles.

  1. 1. Newline Inside the Inner Loop

    Each digit lands on its own line — you get a column, not a triangle.

    → Use echo (9 + $i + $j) . " "; echo PHP_EOL only after the inner loop.

  2. 2. Wrong Formula Offset

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

    → Keep 9 + $i + $j (or $baseVal + $i + $j with $baseVal = 9).

  3. 3. Wrong Inner Bound

    $j <= $rows prints a rectangle — every row has the same width.

    → Keep for ($j = 1; $j <= $i; $j++) so row $i prints $i values.

  4. 4. Missing Trailing Space

    Printing numbers without a space makes multi-digit values run together on wider rows.

    → Append a space after each number: echo (9 + $i + $j) . " ".

  5. 5. Unchecked CLI input

    Letters or empty input leave variables uninitialized.

    → Call is_numeric(trim($line)) and re-prompt on failure.

Edge Cases

Check these inputs before calling the solution done.

rows = 1

Single number row

Output is just 11 — one value, one row.

rows = 0

Empty pattern

Outer loop never runs — print nothing or show a message.

Negative

$rows < 0

Treat as invalid; re-prompt instead of silent empty output.

rows = 2

Smallest triangle

Two rows: 11 and 12 13.

Bad input

Non-numeric CLI input

Unchecked CLI input leaves variables unset — call is_numeric(trim($line)) first.

Large rows

Large row count

Total prints = n(n+1)/2 — grows quadratically with row count.

🎯 Practice Problems

Try these variations to lock in the pattern.

1. Custom base value

  • Replace 9 with a user-entered $baseVal
  • See Example 2 in the gallery

2. Triangle from 1

  • Continue with Program 33
  • Formula $i + $j - 1 instead of 9 + $i + $j

3. Number-star diamond

  • Review Program 31
  • Compare modulus alternation vs arithmetic formula

4. Safe input loop

  • Call is_numeric(trim($line)) until $rows >= 1
  • Then draw the triangle

Notes

  • Formula rule. Each value is 9 + $i + $j. Inner loop runs $j = 1..$i — row $i prints $i numbers.
  • echo (9 + $i + $j) . " " stays on the line; echo PHP_EOL advances — mix them carefully.
  • Validate $rows > 0 for interactive programs; $rows = 1 prints a single 11.
  • Change 9 to any base to shift the whole triangle — compare with Program 33 where the formula is $i + $j - 1.

Quick Takeaway: outer loop $i = 1..$rows, inner $j = 1..$i, print 9 + $i + $j, then echo PHP_EOL.

⏱️ Time and Space Complexity

ProgramTimeExtra space
Nested loops (Examples 1–3)O(n²)O(1)
Smaller demo (Example 3)O(n²)O(1)
Wrap Up

🎉 Conclusion

The increasing number triangle from 11 is a compact lesson in formula-based nested loops: compute each value with 9 + $i + $j, grow the inner bound to $i, and end each row with echo PHP_EOL. Master the fixed-$rows version, then try user input and a smaller trace demo.

Practice the three examples above, then continue to Program 33 for the $i + $j - 1 variant starting at 1.

Inner bound must be $j <= $i — validate $rows when reading from the console.

💡 Best Practices

✅ Do

  • Use for ($i = 1; $i <= $rows; $i++) in the outer loop
  • Inner: for ($j = 1; $j <= $i; $j++) prints $i values
  • Formula: echo (9 + $i + $j) . " "
  • Call is_numeric(trim($line)) before using $rows
  • State O(n²) time when asked about complexity

❌ Don’t

  • Call echo PHP_EOL inside the inner loop
  • Use $j <= $rows in the inner loop (prints a rectangle)
  • Forget the trailing space after each number
  • Ignore bad console input in user-facing demos
  • Skip the $rows = 1 edge case

Key Takeaways

Knowledge Unlocked

Five things to remember about this increasing triangle

Print the pattern the beginner-friendly way.

5
Core concepts
02

Inner bound

$j = 1..$i

Code
+ 03

Base offset

Starts at 11

Code
04

Row break

echo PHP_EOL after $j loop

Shape
O 05

Complexity

O(n²) time

Analysis

❓ Frequently Asked Questions

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 digits separated on the same row. echo PHP_EOL ends the row.
Replace 5 with $rows in the outer loop bound — see Example 2.
O(n²) for n rows because total prints are 1 + 2 + ... + n = n(n+1)/2.
Use trim(fgets(STDIN)) and check is_numeric($input) before casting to int — see Example 2 notes.
Only one row prints — a single 11.
Yes — echo ($baseVal + $i + $j) . " " lets the user pick any starting offset.

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.

Continue to Program 33

Move on to the increasing number triangle starting from 1 ($i + $j - 1) in the PHP number-pattern series.

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