Diamond-Shaped Alphabet Pattern in PHP

Beginner
⏱️ 8 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Two phases

What You'll Learn

This pattern is built by printing the inverted V from program 33, then printing a mirrored copy below it to close the diamond.

⭐ Pattern Output

For 5 rows (A–E):

Output
    A
   B B
  C   C
 D     D
E       E
 D     D
  C   C
   B B
    A
1

Complete PHP Program (A–E)

Fixed version. Uses two-space cells for consistent alignment:

PHP
<?php
$alpha = range('A', 'Z');
$rows = 5; // A..E
$end = $rows - 1;

for ($i = 0; $i <= $end; $i++) {
    for ($j = $end; $j >= 0; $j--) {
        echo ($i === $j) ? ($alpha[$j] . " ") : "  ";
    }
    for ($k = 1; $k <= $end; $k++) {
        echo ($i === $k) ? ($alpha[$k] . " ") : "  ";
    }
    echo PHP_EOL;
}

for ($i = $end - 1; $i >= 0; $i--) {
    for ($j = $end; $j >= 0; $j--) {
        echo ($i === $j) ? ($alpha[$j] . " ") : "  ";
    }
    for ($k = 1; $k <= $end; $k++) {
        echo ($i === $k) ? ($alpha[$k] . " ") : "  ";
    }
    echo PHP_EOL;
}

🧠 How It Works

1

Top half grows to the widest row

$i increases from 0 to $end, so the outline expands until the E row.

2

Bottom half mirrors back down

Start at $end - 1 so the middle row is not duplicated.

3

Same two inner loops for every row

The diamond is formed by reusing identical diagonal logic in both phases.

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

Clamps rows to 26. Here rows means the half-height (top half letters):

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

$alpha = range('A', 'Z');
$end = $rows - 1;

for ($i = 0; $i <= $end; $i++) {
    for ($j = $end; $j >= 0; $j--) {
        echo ($i === $j) ? ($alpha[$j] . " ") : "  ";
    }
    for ($k = 1; $k <= $end; $k++) {
        echo ($i === $k) ? ($alpha[$k] . " ") : "  ";
    }
    echo PHP_EOL;
}

for ($i = $end - 1; $i >= 0; $i--) {
    for ($j = $end; $j >= 0; $j--) {
        echo ($i === $j) ? ($alpha[$j] . " ") : "  ";
    }
    for ($k = 1; $k <= $end; $k++) {
        echo ($i === $k) ? ($alpha[$k] . " ") : "  ";
    }
    echo PHP_EOL;
}

❓ Frequently Asked Questions

It creates an array of uppercase letters so you can print alphabets using simple numeric indexes (0 for A, 1 for B, ...).
PHP_EOL is correct for CLI output and keeps examples consistent for terminal execution. In HTML, you would use <br> instead.
Most pattern programs print O(n) characters per row for n rows, so they are typically O(n2).

Next: PHP Number Pattern Programs

Continue with number pattern tutorials.

Number Patterns →

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