PHP-php字符递增

PHP-php字符递增

晚风撩人 发布于 2016-12-28 字数 95 浏览 1114 回复 2
for($i = 'A'; $i <= 'Z'; $i++) {
echo $i;
}

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜无邪 2017-05-12 2 楼

在处理字符变量的算数运算时,PHP 沿袭了 Perl 的习惯,而非 C 的。例如,在 Perl 中 'Z'+1 将得到 'AA',而在 C 中,'Z'+1 将得到 '['(ord('Z') == 90,ord('[') == 91)。注意字符变量只能递增,不能递减,并且只支持纯字母(a-z 和 A-Z)。

手册中有写!

甜柠檬 2017-03-10 1 楼

php手册里面有这样的解释
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).
Note that character variables can be incremented but not decremented and even so only plain ASCII characters (a-z and A-Z) are supported. Incrementing/decrementing other character variables has no effect, the original string is unchanged.