Increment letters in PHP?

Asked

Viewed 1,561 times

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

3 answers

8


This is typical of weakly typed language such as C for example. One type can be used as if it were another. Note that this is different from being dynamically typed, so much so that C is statically typed (and behaves somewhat differently).

At first there are no problems. It does not cause errors of any kind. It is only recommended to avoid this feature when it is possible because it is easy use wrong. Of course in simple situations like this it is difficult to make a mistake.

Whether it is possible to explicitly say what the intention is (working with characters) is a little better from a readability point of view. Those who do not know this feature may wonder the first code of the question. But to say that is wrong is preciousness.

I know it’s just an example but in this case I would even use is a string literal :)

  • Hard was finding that after the 'z' comes the 'aa', kkkkkkkkkkkkkkkkkkkkkkkkkkkkkk

  • 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.

  • 2

    Technically it makes sense yes, think hexadecimal-like basis. The a would be number 1 and the z would be maiorValor-1. The next number would be A0. Like the 0 does not exist, we use the a even.

4

As already mentioned, this is characteristic of weakly typed languages. this works because increment is done in the code ASCII(A-Z 65-90, a-z 97-122), and not exactly in the letter you see. This works only with increment operator(++). Already with the decreasing is not possible, IE, can not create a string z-a Z-A.

The manual speaks of this behavior, which he follows the convesion Perl which says that the valid set for the increment is (a-z, A-Z and 0-9), so z flipped aa, in C he would follow the next ASCII code.

This feature is useful when mounting the header of a worksheet where the coordinates are ex: A1, B1 ...

Examples in Perl

$char = 'z';
print ++$char; //saída: aa

Equivalent example of crease()

@alfabeto = ('a'..'z');

foreach(@alfabeto){
    print $_
}

0

It is normal to do this way, if I were to need a solution so I would do this way:

foreach (range('a', 'z') as $letra) {
   print $letra;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.