7
Recently I needed to increment letters in PHP within a loop.
For each iteration, instead of numerical indexes, it needed letters of the alphabet.
So, as I already know that PHP does letter incrementing (and because it’s very simple), I did something similar to this.
for($letra = 'a'; $letra != 'aa'; $letra++) {
echo $letra;
}
// abcdefghijklmnopqrstuvwxyz
However, because I have never seen it documented or in any other language I know a feature like this, I was in doubt whether I should use it or not.
Because of coding (and among others), it is safe to use this feature, or it is better to appeal to friends chr
or the range('a', 'z')
, as in the example below?
for($letra = 97; $letra <= 122; $letra++) {
echo chr($letra);
}
//abcdefghijklmnopqrstuvwxyz
echo implode('', range('a', 'z')); //abcdefghijklmnopqrstuvwxyz
//abcdefghijklmnopqrstuvwxyz
Hard was finding that after the
'z'
comes the'aa'
, kkkkkkkkkkkkkkkkkkkkkkkkkkkkkk– Wallace Maxters
To tell the truth it also surprised me. This is PHP’s invention. It may seem practical and is consistent with what PHP does but from the computer’s point of view it doesn’t make much sense.
– Maniero
Technically it makes sense yes, think hexadecimal-like basis. The
a
would be number 1 and thez
would bemaiorValor-1
. The next number would be A0. Like the0
does not exist, we use thea
even.– mutlei