Repeating-Letter Alphabet Triangle in PHP

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested loops

What You'll Learn

Row 1 prints one A, row 2 prints two Bs, row 3 prints three Cs, and so on: A, BB, CCC, DDDD, EEEEE.

This is a great beginner exercise to practice loop boundaries and understand how the outer loop chooses the row’s letter.

⭐ Pattern Output

For rows = 5:

Output
A
BB
CCC
DDDD
EEEEE
1

Complete PHP Program

Fixed rows = 5 version (nested loops):

PHP
<?php
$rows = 5;
$alpha = range('A', 'Z');

for ($i = 0; $i < $rows; $i++) {
    for ($j = 0; $j <= $i; $j++) {
        echo $alpha[$i];
    }
    echo PHP_EOL;
}

🧠 How It Works

1

Build the alphabet array

$alpha = range('A','Z'); gives letters A..Z by index (0 is A, 1 is B, ...).

2

Outer loop chooses the row letter

When $i is 0, the row uses $alpha[0] (A). When $i is 1, the row uses B, and so on.

3

Inner loop controls how many repeats

The inner loop runs $i + 1 times, so the current row letter repeats 1, 2, 3, 4, 5 times.

4

Print the line break

After building one row, print PHP_EOL to move to the next line. This keeps the output readable in CLI output.

Newline
=

Put it together

The outer loop controls rows, the inner loops control what prints on each row, and PHP_EOL separates the lines.

2

Variation — User Input (CLI) Version

Reads rows from standard input and clamps to a max of 26:

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

$alpha = range('A', 'Z');

for ($i = 0; $i < $rows; $i++) {
    for ($j = 0; $j <= $i; $j++) {
        echo $alpha[$i];
    }
    echo PHP_EOL;
}

💡 Tips for Enhancement

Try These

  • Replace the inner loop with str_repeat($alpha[$i], $i + 1)
  • Switch to lowercase using range('a', 'z')
  • Center-align the triangle by printing leading spaces
  • Compare with Program 1 (letters change inside the row)

Avoid

  • Updating the letter inside the inner loop unless you want mixed letters per row
  • Allowing $rows > 26 without handling the alphabet size
  • Using <br> in CLI examples (use PHP_EOL)

Key Takeaways

1

The outer loop selects the row’s letter: A, then B, then C, ...

2

The inner loop repeats that letter i+1 times.

3

Total characters printed is \(n(n+1)/2\), so runtime is O(n²).

❓ Frequently Asked Questions

Because this program prints $alpha[$i] repeatedly for a row. To print AB, ABC, etc., you would print $alpha[$j] inside the inner loop (see Program 1).
Yes. Print $alpha[$i] . ' ' inside the inner loop, or join with spacing using str_repeat plus a separator strategy.

Next: PHP Alphabet Pattern 10

Continue to the next program for another alphabet pattern in PHP.

Program 10 →
Did you know?

You can compute the row letter without an array: chr(ord('A') + $i). Using range() is often easier to read for beginners.

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.

10 people found this page helpful