PHP Descending Number Triangle Pattern (Right-Aligned)
Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview
Definition
What Is This Pattern?
A right-aligned descending number triangle uses a fixed-width inner loop: print spaces while $j > $i, then digits $i..1 — so each row hugs the right edge.
Remember
Rule: for i from 1 to rows
for j from rows down to 1
if j > i: print space
else: print j
1
21
321
4321
54321 ← rows = 5
Follows the spaced mirror in Program 29; next is the number-star diamond in Program 31.
Approach
How to Solve It
Outer loop grows $i. One fixed-width inner loop runs $rows..1 — print a space or a digit so columns stay aligned.
Method
Idea
Best for
Fixed-width if/else
$j > $i → space; else print $j
Learning, interviews, exams
Ternary form
echo ($j > $i) ? " " : $j;
Shorter demos once if/else is clear
Pseudocode
Pseudocode
for i from 1 to rows:
for j from rows down to 1:
if j > i: print space
else: print j
print newline
Cheat sheet
Goal
Pattern
Grow each row
for ($i = 1; $i <= $rows; $i++)
Fixed width
for ($j = $rows; $j >= 1; $j--)
Pad or digit
if ($j > $i) echo " "; else echo $j;
Leading spaces
$rows - $i spaces before digits
End of row
echo PHP_EOL;
Printing Numbers vs Starting a New Line
API
Effect
Use for
echo $j / " "
Stays on the same line
Each space or digit on the row
echo PHP_EOL
Ends the current line
After the fixed-width loop finishes
Print characters without a newline, then end the row once.
Try it
Live Preview
Change the row count and the right-aligned triangle updates instantly — capped at 9 for readable demos.
Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.
Live resultrows = 5 · 25 chars
1
21
321
4321
54321
Trace
Worked Walkthrough — $rows = 5
Trace how leading spaces shrink and descending digits grow on each row.
$i
Spaces
Digits
Prints
1
4
1
1
2
3
21
21
3
2
321
321
4
1
4321
4321
5
0
54321
54321
Leading spaces = $rows − $i. Each row prints $rows characters → O(n²) total.
Code
PHP Programs
Three complete programs: fixed $rows = 5, CLI input with ternary form, and a compact $rows = 3 demo. Use View Output to reveal sample results.
Example 1 — Fixed $rows = 5
Hard-coded height — fixed-width descending loop with if/else for space vs digit.
2. Trace on paper. If the inner loop stops at $i instead of $rows, the triangle loses right alignment.
3. Scale up next. Once the small demo is clear, use Examples 1–2 for five rows or user input.
Edge Cases & Pitfalls
Check these before calling the solution done.
no spaces
Missing else for spaces
Skipping echo " " when $j > $i left-aligns the triangle — you get Program 3’s shape without padding.
width ≠ rows
Loop only to i
Inner loop must always run $rows times. Stopping at $i removes leading spaces and breaks alignment.
j++
Ascending inner loop
for ($j = 1; $j <= $rows; $j++) changes digit order and padding. Keep $j = $rows..1.
PHP_EOL inside
Broken rows
If echo PHP_EOL sits inside the inner loop, you get one character per line. Call it only after the loop.
rows = 1
Single digit
Output is just 1 with no leading spaces. A good sanity check for input validation.
fgets
Check is_numeric
Bare (int) casts turn non-numeric strings into 0 — prefer is_numeric before casting.
Analysis
Time and Space Complexity
Program
Time
Extra space
Fixed / input (Examples 1–2)
O(n²)
O(1)
Compact rows = 3 (Example 3)
O(n²)
O(1)
Each of n rows prints n characters → O(n²) time. Only a few loop variables are needed.
Remember
Key Takeaways
One fixed loop:$j = $rows..1 prints space when $j > $i, else digit $j.
Always width rows: fixed width is what keeps the triangle right-aligned.
Break the row: spaces and digits in the inner loop; echo PHP_EOL after it finishes.
Complexity:O(n²) time from n² characters; O(1) extra space.
One line: for each row $i, walk $j = $rows..1 and print space or $j, then echo PHP_EOL.
Frequently Asked Questions
A right-aligned descending triangle: for rows=5 you get 1 / 21 / 321 / 4321 / 54321 — leading spaces pad each row to width rows.
Leading spaces are printed while $j > $i. Smaller rows get more spaces, pushing digits to the right edge.
The inner loop runs $j from $rows down to 1. When $j <= $i, it prints $j — naturally producing $i..1 on each row.
Running $j from $rows down to 1 on every row keeps column alignment. Spaces fill positions where $j > $i.
Program 3 prints a left-aligned reverse descending triangle with no leading spaces. Program 30 pads with spaces for right alignment.
echo $j (or a space) stays on the same line. echo PHP_EOL ends the row after the fixed-width loop finishes.
Use trim(fgets(STDIN)) and check is_numeric($input) before casting to int — see Example 2.
O(n²) for n rows because each row runs a fixed-width inner loop of n iterations.
🤔
Did you know?
This pattern uses a fixed column width ($rows). For each row $i, the inner loop prints spaces while $j > $i, then prints digits in descending order — producing a right-aligned triangle.