为什么此代码不简单地将字母A打印到Z? [英] Why doesn't this code simply print letters A to Z?

查看:128
本文介绍了为什么此代码不简单地将字母A打印到Z?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<?php
for ($i = 'a'; $i <= 'z'; $i++)
    echo "$i\n";

此代码段提供以下输出(换行符替换为空格):

This snippet gives the following output (newlines are replaced by spaces):

abcdefghijklmnopqrstu vwxyz aa ab ac ad ae af ag ah ai aj ak ak al am ao a p aq ar as at au av aw ax ay az ab ba bb bc bd be bf bg bh bi bj bk bbm bn bo bp bq bq br 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 dk dl dm dn do dp dq ds dt du dv dw dx dz ea ce ee ef EE EE EE EE EE EE EE EE EE EE EE EE EE EE ...

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

推荐答案

来自文档:

在处理字符变量而非C的算术运算时,PHP遵循Perl的约定.

PHP follows Perl's convention when dealing with arithmetic operations on character variables and not C's.

例如,在Perl中,'Z'+1变为'AA',而在C中,'Z'+1变为'['(ord('Z') == 90ord('[') == 91).

For example, in Perl 'Z'+1 turns into 'AA', while in C 'Z'+1 turns into '[' ( ord('Z') == 90, ord('[') == 91 ).

请注意,字符变量可以递增但不能递减,即使如此,仅支持纯ASCII字符(a-z和A-Z).

Note that character variables can be incremented but not decremented and even so only plain ASCII characters (a-z and A-Z) are supported.

来自评论:-
还应注意,<=是词典比较,因此'z'+1 ≤ 'z'. (自'z'+1 = 'aa' ≤ 'z'起.但是'za' ≤ 'z'是第一次比较不正确.)例如,在$i == 'z'可行时中断.

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.

此处的示例.

这篇关于为什么此代码不简单地将字母A打印到Z?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆