所以我在做一些练习并遇到了这段代码(生成“1. Item A”、“2. Item B”等):
echo "\n<ol>";
for ($x='A'; $x<'G'; $x++){
echo "<li>Item $x</li>\n";
}
echo "\n</ol>";
出于好奇,我尝试进行相反的操作(这会产生 Zs 的无限循环):
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
关于php - for循环中递增字符有效,递减不?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33744770/