有同学问了一个问题:
复制代码 代码如下:
php
for($i = 'A'; $i echo $i;
}
输出是啥?
输出是:
abcdefghijklmnopqrstuvwxyzaaabacadaeafagahaiajakalamanaoapaqaras…….
为啥?
其实很简单, php的手册中也有说明, 只不过恐怕很多人不会一章一节的把手册仔细阅读一遍:
php follows perl's convention when dealing with arithmetic operations on character variables and not c's. for example, in perl ‘z'+1 turns into ‘aa', while in c ‘z'+1 turns into ‘[‘ ( ord(‘z') == 90, ord(‘[‘) == 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.
在处理字符变量的算数运算时,php 沿袭了 perl 的习惯,而非 c 的。例如,在 perl 中 ‘z'+1 将得到 ‘aa',而在 c 中,'z'+1 将得到 ‘[‘(ord(‘z') == 90,ord(‘[‘) == 91)。注意字符变量只能递增,不能递减,并且只支持纯字母(a-z 和 a-z)。
也就是说, 如果:
复制代码 代码如下:
$name = "laruence";
++$name; //将会是"laruencf"
而:
复制代码 代码如下:
$name = "laruence";
--$name; //没有影响, 还是"laruence"
所以, 这个问题的原因就是当$i = Z的时候, ++$i成了AA, 而字符串比较的话,
AA,BB,XX一直到YZ都是小于等于Z的… so..
php免费学习视频:立即使用
踏上前端学习之旅,开启通往精通之路!从前端基础到项目实战,循序渐进,一步一个脚印,迈向巅峰!











