Understanding why php increments characters the way it does
-
05-06-2021 - |
Question
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?
Solution
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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow