微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

递增循环中的字符,递减不?

因此,我进行了一些练习并遍历了以下代码(产生“ 1. Item A”,“ 2.Item B”等):

echo "\n<ol>";
for ($x='A'; $x<'G'; $x++){
    echo "<li>Item $x</li>\n";
}
echo "\n</ol>";

很好奇,我尝试做相反的操作(这会产生Z的无限循环):

echo "\n<ol>";
for ($x = 'Z'; $x > 'M'; $x--){
    echo "<li>Item $x</li>\n";
}
echo "\n</ol>";

在这里错过了什么?

解决方法:

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 alphabets and digits (a-z, A-Z and 0-9) are
supported. Incrementing/decrementing other character variables has no
effect, the original string is unchanged.

PHP手册link

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐