Alternating 1-0 Pattern in PHP

Beginner
⏱️ 5 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Modulo Operator

What You’ll Learn

How to print an alternating 1-0 pattern in PHP that grows row by row: 1, 10, 101, 1010, 10101.

We’ll use nested loops and % 2 to generate repeating 1 and 0 digits automatically.

⭐ Pattern Output

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

Output
1
10
101
1010
10101
1

Complete PHP Program

The inner loop counts up from 1, so each row starts with 1 and then alternates using $j % 2.

PHP
<?php
$rows = 5;

for ($i = 1; $i <= $rows; $i++) {
    for ($j = 1; $j <= $i; $j++) {
        echo $j % 2;
    }
    echo PHP_EOL;
}

🧠 How It Works

1

Set the number of rows

$rows = 5; controls the height of the triangle.

Setup
2

Outer loop (rows)

for ($i = 1; $i <= $rows; $i++) picks the current row length $i.

Row control
3

Inner loop (columns)

for ($j = 1; $j <= $i; $j++) runs from 1 to $i, printing one digit per iteration.

Column control
4

Alternate with modulo

echo $j % 2; prints 1 when $j is odd and 0 when it’s even.

Alternation
=

Rows always start with 1

Because the first column is always j = 1, every row begins with 1, then alternates as j increases.

2

Variation — User Input (CLI) Version

Let the user decide the number of rows at runtime using standard input (run from terminal with php):

PHP
<?php
echo "Enter the number of rows: ";
$rows = (int) trim(fgets(STDIN));

if ($rows < 1) {
    echo "Please enter a positive number." . PHP_EOL;
    exit(1);
}

for ($i = 1; $i <= $rows; $i++) {
    for ($j = 1; $j <= $i; $j++) {
        echo $j % 2;
    }
    echo PHP_EOL;
}

💡 Tips for Enhancement

Try These

  • Start each row with 0 by printing ($j + 1) % 2
  • Invert digits with 1 - ($j % 2)
  • Print spaces between digits for readability on big rows
  • Build a fixed-width binary rectangle by keeping columns constant
  • Validate CLI input to prevent 0 or negative rows

Avoid

  • Forgetting the newline after each row
  • Hardcoding outputs (generate with loops instead)
  • Mixing row/column logic (outer loop = rows, inner loop = columns)
  • Ignoring invalid CLI input (empty input becomes 0)

Key Takeaways

1

The outer loop controls the row length from 1 to $rows.

2

The inner loop prints $j % 2 to generate alternating digits.

3

Because $j starts at 1, every row begins with 1.

4

You can create other cycles with % k (like repeating 012 using % 3).

❓ Frequently Asked Questions

Because the inner loop begins at $j = 1, and 1 % 2 is 1.
Use echo ($j + 1) % 2; inside the loop to shift the alternation.
Yes—print 1 - ($j % 2) to flip 0 and 1.
O(n²) for n rows, because the total printed digits are \(1+2+\dots+n\).

Explore More PHP Number Patterns!

Try combining modulo with alignment to build checkerboards, grids, and zig-zag patterns.

All Number Patterns →
Did you know?

You can generate many repeating patterns by changing the modulus. For example, $j % 4 cycles through 1, 2, 3, 0 as $j increases.

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