php 为什么这段代码不简单地打印字母 A 到 Z?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4098345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why doesn't this code simply print letters A to Z?
提问by Milan Babu?kov
<?php
for ($i = 'a'; $i <= 'z'; $i++)
echo "$i\n";
This snippet gives the following output (newlines are replaced by spaces):
此代码段提供以下输出(换行符由空格替换):
a b c d e f g h i j k l m n o p q r s t u v w x y z aa ab ac ad ae af ag ah ai aj ak al am an ao ap aq ar as at au av aw ax ay az ba bb bc bd be bf bg bh bi bj bk bl bm bn bo bp bq br bs bt bu bv bw bx by bz ca cb cc cd ce cf cg ch ci cj ck cl cm cn co cp cq cr cs ct cu cv cw cx cy cz da db dc dd de df dg dh di dj dk dl dm dn do dp dq dr ds dt du dv dw dx dy dz ea eb ec ed ee ef eg eh ei ej ek el em en eo ep eq er es et eu ev ew ex... on to yz
abcdefghijklmnopqrstu vwxyz aa ab ac ad ae af ag ah ai aj ak al am an ao ap aq ar as at au av aw ax ay az ba bb bc bd be bf bg bh bi bj bk bl bm bn bo bp btbu br bv bq bw bx by bz ca cb cc cd ce cf cg ch ci cj ck cl cm cn co cp cq cr cs ct cu cv cw cx cy cz da db dc dd de df dg dh di dj dk dl dm dn do dp dq dr ds du dv dw dx dy dz ea eb eced ee ef eg eh ei eje ek el em en eo ep eq er es et eu ev ew ex...到yz
采纳答案by CMS
From the docs:
从文档:
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 在处理字符变量而不是 C 的算术运算时遵循 Perl 的约定。
例如,在 Perl 中
'Z'+1
变成'AA'
,而在 C 中'Z'+1
变成'['
(ord('Z') == 90
,ord('[') == 91
)。请注意,字符变量可以递增但不能递减,即使如此,也仅支持纯 ASCII 字符(az 和 AZ)。
From Comments:-
It should also be noted that <=
is a lexicographical comparison, so 'z'+1 ≤ 'z'
. (Since 'z'+1 = 'aa' ≤ 'z'
. But 'za' ≤ 'z'
is the first time the comparison is false.) Breaking when $i == 'z'
would work, for instance.
来自评论:-
还应该注意的是,这<=
是一个字典比较,所以'z'+1 ≤ 'z'
. (因为'z'+1 = 'aa' ≤ 'z'
。但这'za' ≤ 'z'
是第一次比较是错误的。)$i == 'z'
例如,打破什么时候会起作用。
回答by Mark Baker
Because once 'z' is reached (and this is a valid result within your range, the $i++ increments it to the next value in sequence), the next value will be 'aa'; and alphabetically, 'aa' is < 'z', so the comparison is never met
因为一旦达到 'z'(这是您范围内的有效结果,$i++ 将它按顺序递增到下一个值),下一个值将是 'aa';并且按字母顺序,'aa' 是 < 'z',所以永远不会满足比较
for ($i = 'a'; $i != 'aa'; $i++)
echo "$i\n";
回答by GreenMatt
Others answers explain the observed behavior of the posted code. Here is one way to do what you want (and it's cleaner code, IMO):
其他答案解释了已发布代码的观察行为。这是做你想做的事情的一种方法(它是更干净的代码,IMO):
foreach (range('a', 'z') as $i)
echo "$i\n";
In response to ShreevatsaR's comment/question about the rangefunction: Yes, it produces the "right endpoint", i.e. the values passed to the function are in the range. To illustrate, the output from the above code was:
回应 ShreevatsaR 关于范围函数的评论/问题:是的,它产生“正确的端点”,即传递给函数的值在范围内。为了说明,上述代码的输出是:
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
回答by Filip Ekberg
Others already said why PHP doesn't show what you expect. Here's how you get the result you might want:
其他人已经说过为什么 PHP 没有显示您期望的内容。以下是您获得可能想要的结果的方法:
<?php
for ($i = ord('a'); $i <= ord('z'); $i++)
echo chr($i);
?>
回答by bcosca
Why not just use range('a','z')
?
为什么不直接使用range('a','z')
?
回答by Chinmay235
Try this code. I think this code will be helpful to you.
试试这个代码。我认为这段代码会对你有所帮助。
$alphas = range('A', 'Z');
foreach($alphas as $value){
echo $value."<br>";
}
Display 26 letters in sequence.
依次显示 26 个字母。
回答by James Dantes
PHP has the function of looping letters and can exceed beyond single characters; the rest will be done this way: aa ab ac... zz, and so on.
PHP具有循环字母的功能,可以超过单个字符;其余的将通过这种方式完成:aa ab ac... zz,依此类推。
Try this:
尝试这个:
<?php
for ($i = 'a'; $i !== 'aa'; $i++)
echo "$i\n";
?>
回答by LRA
Also this can be used:
这也可以使用:
for ($i = 'a'; $i <= 'z'; $i=chr(ord($i)+1))
echo "$i\n";
回答by Mr Griever
<?php
$i = 'a';
do {
echo ($j=$i++),"\r\n";
} while (ord($j) < ord($i));
?>
回答by jon_darkstar
While the above answers are insightful to what's going on, and pretty interesting (I didn't know it would behave like this, and its good to see why.
虽然上面的答案对正在发生的事情很有见地,而且非常有趣(我不知道它会表现得像这样,很高兴知道为什么。
The easiest fix (although perhaps not the most meaningful) would be just to change the condition to $i != 'z'
最简单的解决方法(虽然可能不是最有意义的)只是将条件更改为 $i != 'z'
<?php
for ($i = 'a'; $i != 'z'; $i++)
echo "$i\n";
?>