PHP Alphabet Pattern Programs

Beginner
6 min read
Updated: Nov 2026
3 examples
echo + PHP_EOL

Definition and Usage

A PHP alphabet pattern is a script that prints letters in rows. The outer for loop chooses the last letter of the row. The inner for loop echos each letter, then echo PHP_EOL ends that row.

PHP can increment a one-character string: $letter++ turns 'A' into 'B'. The + operator does not do that. 'A' + $col is addition, so the letter becomes 0. Use chr(ord('A') + $col) when the column is a number.

echo does not end the line.

Each echo $letter stays on the current row. Call echo PHP_EOL once, after the inner loop, to start the next row.

PHP Syntax

Both loops are letters. The inner loop restarts at 'A' and stops when $letter passes $end.

PHP
<?php
for ($end = 'A'; $end <= 'E'; $end++) {
    for ($letter = 'A'; $letter <= $end; $letter++)
        echo $letter;      // same row
    echo PHP_EOL;          // end the row
}

How PHP Prints a Letter

Map each part of the console shape to a PHP value or call.

PiecePHPWhat it does
Letterecho $letterWrites the character and stays on the row
Next letter$letter++Advances 'A' to 'B'
From a columnchr(ord('A') + $col)Code of A plus the column, back to a character
Numeric +'A' + $colAddition: 'A' becomes 0
Concat'A' . $colThe string "A1", not B
New rowecho PHP_EOLEnds the row
Indentstr_repeat(' ', $n)$n spaces
Prefixsubstr($letters, 0, $n)First $n characters of a string

PHP Examples

Three scripts. Each one changes only the PHP that chooses the letter or the leading spaces.

Example 1 — Let PHP advance the letter

The outer loop stops at 'E'. The inner loop reprints from 'A' through the current end letter.

PHP
<?php
for ($end = 'A'; $end <= 'E'; $end++) {
    for ($letter = 'A'; $letter <= $end; $letter++) {
        echo $letter;
    }
    echo PHP_EOL;
}

How It Works

  1. $end++ moves the end letter from A toward E.
  2. The inner loop always restarts at 'A', so every row begins with A.
  3. echo $letter stays on the row. echo PHP_EOL starts the next one.

Example 2 — Build the letter from a column number

ord('A') is the code for A. Add the column, then chr turns that sum back into a letter.

PHP
<?php
$rows = 4;

for ($row = 0; $row < $rows; $row++) {
    for ($col = 0; $col <= $row; $col++) {
        echo chr(ord('A') + $col);
    }
    echo PHP_EOL;
}

How It Works

  1. echo 'A' + $col prints a number, because + casts 'A' to 0.
  2. ord reads the code, the addition stays numeric, and chr returns the letter.
  3. Column 0 is A, column 1 is B, and the row ends with PHP_EOL.

Example 3 — Indent with str_repeat

Print the spaces before the letters. The letter loop is unchanged.

PHP
<?php
$rows = 5;

for ($row = 1; $row <= $rows; $row++) {
    echo str_repeat(' ', $rows - $row);
    for ($letter = 'A'; $letter < chr(ord('A') + $row); $letter++) {
        echo $letter;
    }
    echo PHP_EOL;
}

How It Works

  1. str_repeat(' ', $rows - $row) writes 4 spaces on row 1 and none on row 5.
  2. The inner bound chr(ord('A') + $row) is the first letter that should not print.
  3. echo PHP_EOL still closes the row after the spaces and the letters.

34 PHP Alphabet Pattern Programs

Each card is a full PHP script: nested for loops, a letter for the cell, and echo. Open one for the walkthrough.

Alphabet Pattern 1
Alphabet Pattern 2
Alphabet Pattern 3
Alphabet Pattern 4
Alphabet Pattern 5
Alphabet Pattern 6
Alphabet Pattern 7
Alphabet Pattern 8
Alphabet Pattern 9
Alphabet Pattern 10
Alphabet Pattern 11
Alphabet Pattern 12
Alphabet Pattern 13
Alphabet Pattern 14
Alphabet Pattern 15
Alphabet Pattern 16
Alphabet Pattern 17
Alphabet Pattern 18
Alphabet Pattern 19
Alphabet Pattern 20
Alphabet Pattern 21
Alphabet Pattern 22
Alphabet Pattern 23
Alphabet Pattern 24
Alphabet Pattern 25
Alphabet Pattern 26
Alphabet Pattern 27
Alphabet Pattern 28
Alphabet Pattern 29
Alphabet Pattern 30
Alphabet Pattern 31
Alphabet Pattern 32
Alphabet Pattern 33
Alphabet Pattern 34

Frequently Asked Questions

The + operator adds. PHP casts 'A' to 0, so 'A' + $col is the number $col. Concatenation uses the dot: 'A' . $col is the string "A1", not B. The next letter is chr(ord('A') + $col), or a string you increment with $letter++.
echo writes the letter and stays on the same row. echo PHP_EOL ends the row. "\n" does the same break in a CLI script. If echo PHP_EOL is inside the inner loop, each letter lands on its own line.
Incrementing 'Z' produces 'AA', not the next ASCII character. chr(ord('A') + $n) keeps walking through codes, so column 26 is '['. Keep the row count at 26 or less when the shape should stay inside A–Z.
Yes. Store "ABCDE" in $letters and echo substr($letters, 0, $row) . PHP_EOL. substr returns that prefix, and the dot joins the newline.

Next: Alphabet Pattern 1

A full PHP walkthrough of the right-angled letter triangle, from $letter++ through echo PHP_EOL.

Alphabet Pattern 1 →

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.

28 people found this page helpful