PHP Binary Rows Pattern (Alternating 1 and 0)

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

What Is This Pattern?

An alternating 1 and 0 pattern prints a shrinking triangle where odd rows are all 1s and even rows are all 0s, chosen with $i % 2.

Remember
Rule: odd i → 1, even i → 0; length = rows - i + 1

11111
0000
111
00
1   ← 5 rows

In PHP the outer loop walks each row; the inner loop repeats one character from $j = $i to $rows so the line shrinks each time.

How to Solve It

One nested-loop idea driven by row parity.

MethodIdeaBest for
If/else + moduloEven $i → 0; odd $i → 1Learning, interviews, exams
Ternary formSame parity check in one expressionShorter demos

Pseudocode

Pseudocode
for i from 1 to rows:
    for j from i to rows:
        if i is even: print 0 else print 1
    print newline

Cheat sheet

GoalPattern
Walk each rowfor ($i = 1; $i <= $rows; $i++)
Shrink the rowfor ($j = $i; $j <= $rows; $j++)
Pick 1 or 0if ($i % 2 == 0) echo "0"; else echo "1";
Ternary formecho $i % 2 == 0 ? "0" : "1";
End the rowecho PHP_EOL;

Printing Numbers vs Starting a New Line

APIEffectUse for
echo "1" / echo "0"Stays on the same lineEach 1 or 0
echo PHP_EOLEnds the current lineAfter the inner loop

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

Live Preview

Change the row count and the alternating binary triangle updates instantly.

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

Live result 5 rows · 15 chars
11111
0000
111
00
1

Worked Walkthrough — $rows = 4

Trace parity and length. Odd rows use 1; even rows use 0.

$iParity / countPrinted row
1odd → 1 × 41111
2even → 0 × 3000
3odd → 1 × 211
4even → 0 × 10

Row length is $rows - $i + 1. The character depends only on $i, not on $j.

PHP Programs

Three complete programs: fixed height with if/else, CLI input with a ternary, and a compact 3-row demo. Use View Output for sample results.

Example 1 — Fixed $rows = 5

Shrinking inner loop; even rows print 0, odd rows print 1.

PHP
<?php
$rows = 5;

for ($i = 1; $i <= $rows; $i++) {
    for ($j = $i; $j <= $rows; $j++) {
        if ($i % 2 == 0)
            echo "0";
        else
            echo "1";
    }

    echo PHP_EOL;
}

How It Works

1. Outer loop. $i grows from 1 to 5 and decides both the parity and the start of the inner loop.

2. Parity. $i % 2 == 0 means even → print 0; otherwise print 1.

3. Inner loop. $j runs from $i to $rows, repeating that character $rows - $i + 1 times.

Example 2 — User Input

Read the row count from STDIN and use a ternary for compact 1-or-0 logic.

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

if (!is_numeric($input) || (int) $input < 1) {
    echo "Please enter a positive whole number." . PHP_EOL;
    exit(1);
}

$rows = (int) $input;

for ($i = 1; $i <= $rows; $i++) {
    for ($j = $i; $j <= $rows; $j++)
        echo $i % 2 == 0 ? "0" : "1";

    echo PHP_EOL;
}

How It Works

1. Validate rows. Check is_numeric; require $rows >= 1.

2. Same pattern. Four rows end on a single 0.

Example 3 — Compact $rows = 3

A smaller fixed demo — same parity-and-shrink idea, easier to trace by hand.

PHP
<?php
$rows = 3;

for ($i = 1; $i <= $rows; $i++) {
    for ($j = $i; $j <= $rows; $j++) {
        if ($i % 2 == 0) echo "0";
        else echo "1";
    }

    echo PHP_EOL;
}

How It Works

1. Same rules. Odd rows print 1; even rows print 0; length shrinks each time.

2. Quick check. Three rows end on a single 1.

Edge Cases & Pitfalls

Check these before calling the solution done.

flip parity

Swap even/odd outputs

Printing 0 on odd rows flips the pattern to start with zeros. Keep even → 0, odd → 1.

wrong length

Use $j = 1..$i instead

That grows rows instead of shrinking them. Keep for ($j = $i; $j <= $rows; $j++).

check $j

Test $j % 2 instead of $i % 2

That alternates inside the row (10101). This pattern uses one character per entire row.

rows = 1

Single 1

Output is just 1 — one odd row of length 1.

rows ≤ 0

Empty output

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

Bad input

(int) alone is not enough

Prefer is_numeric after trim(fgets(STDIN)) so non-numeric input does not silently become 0.

Time and Space Complexity

ProgramTimeExtra space
If/else (Examples 1, 3)O(n²)O(1)
Ternary form (Example 2)O(n²)O(1)

Total printed characters are n + (n-1) + … + 1 = n(n+1)/2, which is quadratic in n.

Key Takeaways

  • Rule: odd $i prints 1; even $i prints 0.
  • Length: row $i has $rows - $i + 1 characters via $j = $i..$rows.
  • echo vs PHP_EOL: digits stay on the line; echo PHP_EOL advances after each row.
  • Next step: Program 41 prints square numbers in a pyramid pattern.

One line: for each row, pick 1 or 0 from $i % 2 and repeat it while the row length shrinks.

Frequently Asked Questions

It checks $i % 2. When $i is even, the row prints 0; when $i is odd, the row prints 1.
The inner loop runs from $j = $i to $rows, printing $rows - $i + 1 characters per row — decreasing from $rows down to 1.
Swap the if/else outputs, or invert the condition so odd rows print 0 and even rows print 1.
Program 39 rotates digits 1..$rows per row. Program 40 prints only 1 or 0 per row based on parity, with shrinking row length.
Replace 5 with $rows in the outer loop bound — see Example 2.
Use echo ($i % 2 == 0 ? "0 " : "1 ") instead of echo without a space.
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.
Only one row prints — a single 1 on one line.

Did you know?

Odd rows print 1, even rows print 0 — chosen with $i % 2. Row $i prints $rows - $i + 1 characters; total prints = n(n+1)/2.

Next: Square Numbers Pyramid

Print perfect squares arranged in a growing pyramid.

Program 41 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.

6 people found this page helpful