<?php
for ($i = 'a'; $i <= 'z'; $i++)
echo "$i\n";
此代码段给出以下输出(换行符被空格替换):
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
最佳答案
来自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.
From Comments:-
还应注意 <=
是一个字典比较,所以 'z'+1 ≤ 'z'
. (从 'z'+1 = 'aa' ≤ 'z'
开始。但是 'za' ≤ 'z'
是第一次比较为假。)在 $i == 'z'
时中断例如,会起作用。
关于php - 为什么这段代码不简单地打印字母 A 到 Z?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4098345/