PHP Binary Rows Pattern (Alternating 1 and 0)

Definition
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.
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.
Approach
How to Solve It
One nested-loop idea driven by row parity.
| Method | Idea | Best for |
|---|
| If/else + modulo | Even $i → 0; odd $i → 1 | Learning, interviews, exams |
| Ternary form | Same parity check in one expression | Shorter demos |
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
| Goal | Pattern |
|---|
| Walk each row | for ($i = 1; $i <= $rows; $i++) |
| Shrink the row | for ($j = $i; $j <= $rows; $j++) |
| Pick 1 or 0 | if ($i % 2 == 0) echo "0"; else echo "1"; |
| Ternary form | echo $i % 2 == 0 ? "0" : "1"; |
| End the row | echo PHP_EOL; |
Printing Numbers vs Starting a New Line
| API | Effect | Use for |
|---|
echo "1" / echo "0" | Stays on the same line | Each 1 or 0 |
echo PHP_EOL | Ends the current line | After the inner loop |
Print characters without a newline, then end the row once.
Try it
Live Preview
Change the row count and the alternating binary triangle updates instantly.
Trace
Worked Walkthrough — $rows = 4
Trace parity and length. Odd rows use 1; even rows use 0.
$i | Parity / count | Printed row |
|---|
1 | odd → 1 × 4 | 1111 |
2 | even → 0 × 3 | 000 |
3 | odd → 1 × 2 | 11 |
4 | even → 0 × 1 | 0 |
Row length is $rows - $i + 1. The character depends only on $i, not on $j.
Code
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
$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
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
$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 paritySwap even/odd outputs
Printing 0 on odd rows flips the pattern to start with zeros. Keep even → 0, odd → 1.
wrong lengthUse $j = 1..$i instead
That grows rows instead of shrinking them. Keep for ($j = $i; $j <= $rows; $j++).
check $jTest $j % 2 instead of $i % 2
That alternates inside the row (10101). This pattern uses one character per entire row.
rows = 1Single 1
Output is just 1 — one odd row of length 1.
rows ≤ 0Empty 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.
Analysis
Time and Space Complexity
| Program | Time | Extra 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.
Remember
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.
About the author
Developer, cloud engineer, and technical writer
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