|
Posted by Janwillem Borleffs on 08/19/06 11:11
Dennis de Wit wrote:
> Just figured out... $a++ has a different meaning from $a+=1 when used
> on a string. Don't ask me why anyone would use number calculations on
> strings, but why the hell does ++ behave different on this matter?
>
> Why can't i say 'aa' + 'bb' and make it turn into 'cc'? That would be
> the same logic!
>
When you do $a += 1, PHP knows that you want to add the integer 1 to $a.
Therefore $a + 1 equals 0 + 1 = 1 (as the string 'a' is converted to a 0,
because it doesn't represent a valid integer value).
With $a++, PHP will behave like it gets $a's numeric representation as its
ASCII value and adds 1. The following will print the alphabet for you:
$a = 'A';
print $a;
while ('Z' != $a) {
$a++;
print $a;
}
Note that this only works for a-z & A-Z; if you try it on, say, '@', the
value will remain unchanged.
FYI, Perl behaves the same with the a-z & A-Z ranges, but returns 1 for the
$a++ operation with other characters.
JW
Navigation:
[Reply to this message]
|