3

PHP Manual states: PHP follows Perl's convention when dealing with arithmetic operations on character variables and not C's. For example, in PHP and Perl $a = 'Z'; $a++; turns $a into 'AA', while in C a = 'Z'; a++; turns a into '[' (ASCII value of 'Z' is 90, ASCII value of '[' is 91).

If PHP converts the characters to ascii values (assuming) when dealing with arithmetic operations on characters, should it not print '[' instead of AA? Why and how does PHP increment characters the way it does?

user_stackoverflow
  • 734
  • 2
  • 10
  • 18
  • 2
    Why?, because thats the way they chose it do it. How?, read the source. – Petah May 02 '12 at 01:23
  • Oh, and its basically base 26 counting. – Petah May 02 '12 at 01:24
  • At it says, it follows Perl's conventions. That means it does *not* treat characters by their ASCII value. The way it works is not a byproduct of some weird quirk, it's a deliberate choice. – deceze May 02 '12 at 01:34

1 Answers1

2

If PHP converts the characters to ascii values (assuming) when dealing with arithmetic operations on characters ...

Your assumption is false, since it treats "0" and 0 as equal, instead of "0" and 48.

$ php
<?php
echo "0" == 0 ; echo "\n";
echo "0" == 48 ; echo "\n";
1
​
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358