Friday, October 17, 2025
HomeLanguagesHow to increment letters like numbers in PHP ?

How to increment letters like numbers in PHP ?

Given some letters and the task is to increment letters like we increment numbers. We will encounter various cases and establish a result.

Example:

  • Numbers after increment
    0 1 2 3...
  • Letters after increment
    a b c d...

One more interesting thing to notice is that just like numbers start with double-digit after 9 letters do the same after encountering ‘z’

  • Numbers:
    0 1 2 3 ... 9 10 11 12 .. . 99 100 101 ...
  • Letters:
    a b c d ... z aa ab ac ... zz aaa aab ...

This can be carried out using the simple increment (++) operator just like in numbers. The only point of difference being decrement (–) operator doesn’t work the same in letters as it does in numbers.

Example 1: Program to increment various letters and print it.




<?php
$i = 'a';
print(++$i . " ");
  
$j = 'aa';
print(++$j . " ");
  
$k = 'aaa';
print(++$k . " ");
  
$l = 'aaaa';
print(++$l);
?>


Output:

b ab aab aaab

Example 2: Program to print all letters between ‘a’ to ‘y’.




<?php
$i = 'a';
  
for( $i; $i < 'z'; $i++ )
    print($i);
?>


Output: This example is looped till ‘y’ because if the limit in for is set until it reaches ‘z’, the required result is different. The loop executes until it encounters ‘z’

abcdefghijklmnopqrstuvwxy

Note: Decrement doesn’t work on letters

Program 3: Program to show that decrement doesn’t work on letters.




<?php
$i = 'd';
  
for( $i; $i > 'a'; $i-- )
    print($i);
?>


Output:

The following program produces an infinite loop of letter 'd'
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS