PHP Number Pattern Programs

Beginner
6 min read
Updated: Nov 2026
3 examples
echo + PHP_EOL

Definition and Usage

A PHP number pattern is a script that prints integers in rows. The outer for loop chooses the row. The inner for loop echos each number, then echo PHP_EOL ends that row.

echo does not add a newline, so single digits sit next to each other (123). Two calls with no separator glue wider values: echo 10; echo 11; prints 1011. The dot does the same, because 10 . 11 is the string "1011". 10 + 11 is addition, so that expression prints 21.

The loop variable is the number.

Echo $col for 1, 2, 3… Echo $row to repeat the same digit. A separate $value that you increment is how a running sequence crosses from one row to the next.

PHP Syntax

Count the outer loop down when the first row should be the longest. The inner loop still walks 1 through the current width.

PHP
<?php
for ($row = $rows; $row >= 1; $row--) {
    for ($n = 1; $n <= $row; $n++)
        echo $n;          // digits, no space
    echo PHP_EOL;         // end the row
}

How PHP Prints a Number

Map each part of the console shape to a PHP value or call.

PiecePHPWhat it does
Digitecho $nWrites the number and stays on the row
Same digitecho $rowRepeats the row number on that line
Next value$value++Keeps counting across columns and rows
Join10 . 11The string "1011"
Add10 + 11The number 21
New rowecho PHP_EOLEnds the row
Widthprintf("%2d ", $n)Two columns, then a space
Odd or even$n % 20/1 patterns from the remainder
One digitchr(ord('0') + $n)One character for 0–9

PHP Examples

Three scripts. Each one changes only the PHP that chooses the number or its width.

Example 1 — Shrink the row with the outer loop

Start $row at 5 and stop at 1. The inner loop echoes 1 through $row.

PHP
<?php
$rows = 5;

for ($row = $rows; $row >= 1; $row--) {
    for ($n = 1; $n <= $row; $n++) {
        echo $n;
    }
    echo PHP_EOL;
}

How It Works

  1. $row-- makes each later line one digit shorter.
  2. The inner for always restarts at 1, so every row begins with 1.
  3. echo $n stays on the row. echo PHP_EOL starts the next one.

Example 2 — Echo the row number, not the column

The inner loop only decides how many times to echo. The value is $row.

PHP
<?php
$rows = 5;

for ($row = 1; $row <= $rows; $row++) {
    for ($col = 1; $col <= $row; $col++) {
        echo $row;
    }
    echo PHP_EOL;
}

How It Works

  1. Row 1 runs the inner loop once and echoes 1.
  2. Row 3 runs it three times and echoes 3 each time.
  3. Swap echo $row for echo $col and the line becomes 123.

Example 3 — Keep two-digit values aligned

printf("%2d ", $value) reserves two columns and adds the gap. PHP_EOL still ends the row.

PHP
<?php
$value = 1;

for ($row = 1; $row <= 4; $row++) {
    for ($col = 1; $col <= $row; $col++) {
        printf("%2d ", $value);
        $value++;
    }
    echo PHP_EOL;
}

How It Works

  1. $value lives outside both loops, so counting continues on the next row.
  2. %2d turns 1 into two columns and leaves 10 as two digits.
  3. The space in the format is separate from the width. echo PHP_EOL closes the row.

62 PHP Number Pattern Programs

Each card is a full PHP script: nested for loops, a number for the cell, and echo. Open one for the walkthrough.

Number Pattern 1
Number Pattern 2
Number Pattern 3
Number Pattern 4
Number Pattern 5
Number Pattern 6
Number Pattern 7
Number Pattern 8
Number Pattern 9
Number Pattern 10
Number Pattern 11
Number Pattern 12
Number Pattern 13
Number Pattern 14
Number Pattern 15
Number Pattern 16
Number Pattern 17
Number Pattern 18
Number Pattern 19
Number Pattern 20
Number Pattern 21
Number Pattern 22
Number Pattern 23
Number Pattern 24
Number Pattern 25
Number Pattern 26
Number Pattern 27
Number Pattern 28
Number Pattern 29
Number Pattern 30
Number Pattern 31
Number Pattern 32
Number Pattern 33
Number Pattern 34
Number Pattern 35
Number Pattern 36
Number Pattern 37
Number Pattern 38
Number Pattern 39
Number Pattern 40
Number Pattern 41
Number Pattern 42
Number Pattern 43
Number Pattern 44
Number Pattern 45
Number Pattern 46
Number Pattern 47
Number Pattern 48
Number Pattern 49
Number Pattern 50
Number Pattern 51
Number Pattern 52
Number Pattern 53
Number Pattern 54
Number Pattern 55
Number Pattern 56
Number Pattern 57
Number Pattern 58
Number Pattern 59
Number Pattern 60
Number Pattern 61
Number Pattern 62

Frequently Asked Questions

echo 10; echo 11; writes the digits with no separator, so the row is 1011. The dot does the same: 10 . 11 is the string "1011". Addition is different: 10 + 11 is 21. Separate columns with echo $n . " " or printf("%2d ", $n).
echo $n writes the number and stays on the same row. echo PHP_EOL ends the row. "\n" does the same break in a CLI script. If echo PHP_EOL is inside the inner loop, each digit lands on its own line.
printf("%2d ", $n) reserves two columns for that call, then writes a space. It only affects that value. str_pad((string) $n, 2, " ", STR_PAD_LEFT) pads the string form the same way. echo PHP_EOL still ends the row.
Yes, when the value is 0 through 9. chr(ord('0') + $n) is one character. echo $n still works, and it is the call you need once the value reaches 10.

Next: Number Pattern 1

A full PHP walkthrough of the descending number triangle, from echo $n through echo PHP_EOL.

Number Pattern 1 →

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.

28 people found this page helpful