php - for循环中递增字符有效,递减不?

标签 php loops for-loop increment chars

所以我在做一些练习并遇到了这段代码(生成“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/

相关文章:

php - 使用 php 在数据库中自动增加表的下一个

php - 为什么字符串大于或小于整数?

php - CodeIgniter MYSQLI 获取不同于

python - 优化python中的双循环

php - 使用 mysql 数组循环有限数量的 html

javascript - 无法将数组作为数组进行操作

javascript - 在语法 For 循环中定义循环条件数组/对象

javascript - Highcharts 在 X 轴上显示月份日期(来自 JSON 文件)。 tickInterval : not work?

jquery - 视频作为背景,如何循环播放?

python - 循环遍历字典时删除和添加键