How to increment letters like numbers in PHP?

✔ Recommended Answer

Character/string increment works in PHP (though decrement doesn't)

$x = 'AAZ';$x++;echo $x;  // 'ABA'

Source: stackoverflow.com

Answered By: Mark Baker

Method #2

In PHP, you can increment letters like numbers by first converting the letters into their corresponding ASCII values, incrementing the ASCII values, and then converting them back to letters. Here's an example:

php
$letter = 'a'; // Starting letter $letter++; echo $letter; // Output: b

In the above code, $letter is initially set to the letter 'a'. When $letter++ is executed, the letter is converted to its ASCII value (97), incremented by 1, and then converted back to a letter ('b').

You can use this method to increment letters in PHP just like you would with numbers.

Comments

Most Popular

Remove Unicode Zero Width Space PHP

PhpStorm, return value is expected to be 'A', 'object' returned

Laravel file upload returns forbidden 403, file permission is 700 not 755