An example of increasing and decrementing PHP strings
- 2020-12-22 17:36:26
- OfStack
Today I saw this paragraph in the php manual:
"When dealing with arithmetic operations on character variables, PHP follows the habits of Perl, not C. For example, in Perl $a = 'Z'; $a++; This will change $a to 'AA', in which case, a = 'Z'; a++; Will change a to '[' (the ASCII value for 'Z' is 90, and the ASCII value for '[' is 91). Note that character variables can only be incremented, not decremented, and only pure letters are supported (a-ES21en and ES22en-ES23en). Incrementing/decrementing other character variables is invalid, as the original string does not change."
In other words:
The result: ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASATAUA...
Also string variables cannot be decremented:
This also means that $a++ or ++$a, $a = $a + 1; To explain
"When dealing with arithmetic operations on character variables, PHP follows the habits of Perl, not C. For example, in Perl $a = 'Z'; $a++; This will change $a to 'AA', in which case, a = 'Z'; a++; Will change a to '[' (the ASCII value for 'Z' is 90, and the ASCII value for '[' is 91). Note that character variables can only be incremented, not decremented, and only pure letters are supported (a-ES21en and ES22en-ES23en). Incrementing/decrementing other character variables is invalid, as the original string does not change."
In other words:
for($i = 'A'; $i <= 'Z'; $i++) {
echo $i;
//if( $i == 'ZZZ') die();
}
The result: ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASATAUA...
Also string variables cannot be decremented:
$a = 'Z';
--$a;
echo $a; // Z
This also means that $a++ or ++$a, $a = $a + 1; To explain
$a = $b = 'Z';
$a = $a + 1;
echo $a; //1
++$b;
echo $b; //AA