$a = 'a';
print ($a+1);
print ($a++);
print $a;
The output is: 1 a b
So it is clear that increment operator did its job but I don't understand why output is '1' in case $a+1
. Can anyone explain?
$a = 'a';
print ($a+1);
print ($a++);
print $a;
The output is: 1 a b
So it is clear that increment operator did its job but I don't understand why output is '1' in case $a+1
. Can anyone explain?
PHP is not C, so 'a' + 1
is not 'b'
.
'a'
in a numeric context is 0
, and 0+1 = 1
.
php> echo (int)'a';
0
The fact that the postfix/prefix increment operators do work like if it was a C char seems to be a nasty "feature" of PHP. Especially since the decrement operators are a no-op in this case.
When you increment 'z'
it gets even worse:
php> $a = 'z';
php> echo ++$a
aa