Number Pattern 34 in PHP

Beginner
⏱️ 5 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

What You’ll Learn

How to print Number Pattern 34 in PHP using nested loops. The loops start from 0, and each value is computed as $i + $j.

This is a great exercise to see how changing loop start values (0-based indexing) affects the printed output.

⭐ Pattern Output

For $rows = 5, the pattern looks like this:

Output
0
1 2
2 3 4
3 4 5 6
4 5 6 7 8
5 6 7 8 9 10
1

Complete PHP Program

The outer loop runs from 0 to $rows. For each row $i, the inner loop prints values from $i + 0 up to $i + $i.

PHP
<?php
$rows = 5;

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

🧠 How It Works

1

Choose the maximum row index

$rows = 5; prints rows from 0 through 5 (total 6 rows).

Setup
2

Outer loop (rows 0..$rows)

for ($i = 0; $i <= $rows; $i++) controls which row is being printed.

Row control
3

Inner loop (columns 0..$i)

for ($j = 0; $j <= $i; $j++) prints $i+1 values on row $i.

Column control
4

Compute and print $i + $j

echo ($i + $j) . " "; starts each row at $i and increments by 1 across the row.

Value formula
=

0-based growing triangle

Total printed values are 1+2+…+n = n(n+1)/2, so time complexity is O(n²) for n rows.

2

Variation — User Input (CLI) Version

Let the user decide the maximum row index at runtime using standard input (run from terminal with php):

PHP
<?php
echo "Enter the maximum row index: ";
$rows = (int) trim(fgets(STDIN));

if ($rows < 0) {
    exit("Row index must be 0 or greater." . PHP_EOL);
}

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

💡 Tips for Enhancement

Try These

  • Keep 0-based loops to practice indexing, or switch to 1-based loops for more common patterns
  • Remove the trailing space by building each row as a string and trimming it
  • Right-align the triangle by printing leading spaces before each row
  • Change the formula to create different diagonals (for example, $i + 2*$j)
  • Print the same pattern using arrays instead of echoing directly

Avoid

  • Using negative row indices without validation
  • Forgetting the newline after each row (PHP_EOL)
  • Mixing row/column responsibilities (outer loop = rows, inner loop = columns)
  • Assuming CLI input is always valid

Key Takeaways

1

The outer loop counts from 0 to $rows, producing $rows+1 rows.

2

The inner loop prints $i+1 values on row $i (columns 0..$i).

3

Each value is computed using $i + $j, so the row starts at $i and increases.

4

Total printed values are n(n+1)/2, giving O(n²) runtime.

❓ Frequently Asked Questions

On row $i=1, the inner loop prints $i+$j for $j=0 and $j=1, which gives 1 and 2.
Yes. If you want 5 rows total, set $rows = 4 because the loop includes row 0.
Start $i and $j from 1 and print $i + $j - 1. That produces 1, then 2 3, then 3 4 5, and so on.
O(n²) for n rows because the program prints 1+2+…+n values, which equals n(n+1)/2.

Explore More PHP Number Patterns!

Practice nested loops with progressively richer number patterns and variations.

All Number Patterns →
Did you know?

With 0-based indices, each row starts at the same value as the row index because the first column has $j=0, making the first value $i + 0.

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