PHP Reverse Alphabet Triangle Pattern

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

What Is This Pattern?

A reverse alphabet right-angled triangle prints letters from a top letter down toward A, growing one character longer each row — the descending mirror of Program 1.

Remember
Rule: each row prints top..i (descending); i walks top..A

E
ED
EDC
EDCB
EDCBA     ← 5 rows (top = E)

The inner loop always starts at $top. Only the ending letter $i moves down each row, so every line begins with the same letter and grows toward A.

How to Solve It

Count the end letter down from the top, then print from the top down to that end on each row.

MethodIdeaBest for
Char countdownOuter $i from top..A; inner $j from top..iLearning, interviews, fixed demos
Row count$top = chr(ord('A') + $rows - 1), then same loopsUser input for height

Pseudocode

Pseudocode
$top = chr(ord('A') + $rows - 1)
for $i from $top down to 'A':
    for $j from $top down to $i:
        echo $j (no newline)
    echo PHP_EOL

Cheat sheet

GoalPattern
Pick the end letterfor ($i = $top; $i >= 'A'; $i--)
Print top..ifor ($j = $top; $j >= $i; $j--) echo $j;
Compute top from rows$top = chr(ord('A') + $rows - 1);
Spaced lettersecho $j . " ";
End the rowecho PHP_EOL;
A–Z-safe heightClamp $rows to 1–26

Printing Letters vs Starting a New Line

APIEffectUse for
echoStays on the same lineEach letter on the row
echo PHP_EOLEnds the current lineAfter the inner loop

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

Live Preview

Change the row count and the reverse triangle updates instantly — including top letter and letter totals.

Whole numbers from 1 to 10. Top letter is A + rows - 1; total letters equal n(n+1)/2.

Live result 5 rows · top E · 15 letters
E
ED
EDC
EDCB
EDCBA

Worked Walkthrough — $top = 'E'

Trace each end letter $i and the descending run printed from E down to $i.

$iInner $jPrinted rowCount
EE..EE1
DE..DED2
CE..CEDC3
BE..BEDCB4
AE..AEDCBA5

Total letters: 1 + 2 + 3 + 4 + 5 = 15 = n(n+1)/2. That triangular sum is why time is O(n²).

PHP Programs

Three complete programs: fixed top E, CLI row-count input, and spaced letters. Use View Output to reveal sample results.

Example 1 — Fixed Top E

Outer loop sets the last letter for each row (E, D, C, B, A). Inner loop prints from 'E' down to that letter.

PHP
<?php
for ($i = 'E'; $i >= 'A'; $i--) {
    for ($j = 'E'; $j >= $i; $j--) {
        echo $j;
    }
    echo PHP_EOL;
}
?>

How It Works

1. Outer loop picks the end. $i walks E, D, C, B, A — the last letter printed on that row.

2. Inner loop always starts at top. Print from 'E' down to $i. When $i = 'C', that is EDC.

3. Break the line. echo PHP_EOL; after the inner loop starts the next longer reverse run.

Example 2 — Row Count Input

Read the number of rows and compute $top = chr(ord('A') + $rows - 1). Prefer is_numeric in real apps.

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

$top = chr(ord('A') + $rows - 1);
for ($i = $top; $i >= 'A'; $i--) {
    for ($j = $top; $j >= $i; $j--) {
        echo $j;
    }
    echo PHP_EOL;
}
?>

How It Works

1. Map rows → top. For 4 rows, $top becomes 'D'.

2. Same nested-loop core. Only the bounds follow $top instead of the literal 'E'.

3. Safer input tip. Prefer is_numeric and stay within A–Z:

Safer input
$input = trim(fgets(STDIN));
if (!is_numeric($input)) {
    echo "Enter a row count from 1 to 26." . PHP_EOL;
    exit;
}
$rows = (int) $input;
if ($rows < 1 || $rows > 26) {
    echo "Enter a row count from 1 to 26." . PHP_EOL;
    exit;
}

Example 3 — Spaced Letters

Print a trailing space after each letter so columns are easier to scan.

PHP
<?php
$top = 'E';

for ($i = $top; $i >= 'A'; $i--) {
    for ($j = $top; $j >= $i; $j--) {
        echo $j . " ";
    }
    echo PHP_EOL;
}
?>

How It Works

1. Same bounds. Outer and inner loops match Example 1 — only the printed unit changes.

2. Letter plus space. $j . " " makes columns easier to read at a glance.

3. Compact tip. Trim trailing spaces later if you need a tight line without a final space.

Edge Cases & Pitfalls

Check these before calling the solution done.

Inner starts at A

Wrong order

Starting the inner loop at 'A' and counting up prints the forward triangle (Program 1), not this reverse pattern.

$j from $i

Missing top letter

Inner must start at $top, not $i. Starting at $i prints a single letter per row.

PHP_EOL early

Broken row

Call echo PHP_EOL only after the inner loop. Inside it, each letter lands on its own line.

$rows = 1

Single A

Output is just A — a good sanity check.

Past Z

Clamp to 26

More than 26 rows walks $top past Z. Cap or reject the input.

Bad parse

Use is_numeric

Casting non-numeric input to int becomes 0. Prefer is_numeric so empty or letter input does not silently fail.

Time and Space Complexity

ProgramTimeExtra space
Fixed / input / spaced formsO(n²)O(1)

Total letters printed = 1 + 2 + … + n = n(n+1)/2, which is quadratic in the number of rows.

Key Takeaways

  • Always start at top: inner loop prints top..i, never from A upward.
  • End letter walks down: outer $i from top..A grows each reverse row.
  • Mirror of Program 1: same triangle shape, descending letter order.
  • Complexity: O(n²) time; O(1) extra space.

One line: for each end letter counting down, echo top..end, then echo PHP_EOL.

Frequently Asked Questions

The outer loop sets the last letter on each row from E down to A. For each outer letter i, the inner loop starts at E and prints down to i, so rows grow longer while letters remain in reverse order.
Because the inner loop always starts at top (E in the fixed example). Only the end letter changes with the outer-loop bound, so the triangle grows by one character each row.
Yes. Read rows from the console and set $top = chr(ord('A') + $rows - 1). Then loop $i from $top down to 'A' and echo $j from $top down to $i.
Use Program 1: loop upward from 'A' and print to the current end letter. This page is the descending mirror of that pattern.
echo stays on the same line for each letter. echo PHP_EOL ends the row after the inner loop finishes.
O(n²) for n rows, because the total printed letters are 1+2+...+n = n(n+1)/2.
Use trim(fgets(STDIN)) and check is_numeric($input) before casting to int, require n ≥ 1, and cap at 26 so the top letter stays within A–Z.
Yes. Use 'a' as the base: $top = chr(ord('a') + $rows - 1), then loop the same way downward.

Did you know?

This reverse right-angled alphabet triangle prints letters from a top letter down to A on each row. For 5 rows, the output is E, ED, EDC, EDCB, and EDCBA. In PHP, step letters with $i-- or chr(ord($i) - 1) — for example chr(ord('E') - 1) becomes 'D'.

Next: Reverse Starting Letter

Each row begins at a different letter counting down toward A.

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

12 people found this page helpful