试着用php编写一个简单的字母程序

时间:2010-12-20 04:59:20

标签: php arrays loops

我试图从一行A-Z上的数组中吐出字母表中的每个字母。

这是我的代码到目前为止的样子:

$alphabet = array ("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");

while ($alphabet) {
   echo $alphabet;
   $alphabet;
}

我有点卡在这个部分,并不太确定还有什么可以写这个工作。有什么建议吗?

4 个答案:

答案 0 :(得分:10)

使用rangearray_walk

function e($s) { echo $s; }
array_walk(range('A', 'Z'), 'e');

工作示例:http://codepad.org/pedjOlY9

答案 1 :(得分:1)

$alphabet = array ("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");

foreach($alphabet as $letter) {
echo $letter;
}

答案 2 :(得分:0)

我不确定你为什么需要这个阵列...... 这就是我们拥有ASCII码的原因。 你可以这样做:

for ($i = 65; $i <=90; $i++)
{
    echo chr($i) . PHP_EOL;
}

chr()显示ASCII地图中的字符 - 请在此处查看:http://www.danshort.com/ASCIImap/。如果你想做小写 - 只需使用strtolower()或97-122之间的数字。 PHP_EOL是一个内置常量,可以输出行尾。您可以使用。“
”进行更改。如果您正在使用HTML。

我认为范围要稍微长一些,但它仍然有效。

答案 3 :(得分:0)

这可能对您有所帮助

$alphabet = array ("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");

$c = sizeof($alphabet);    
for($i= 0; $i < $c ; $i++) {    
    echo $alphabet[$i];    
}

你可以使用count($ alphabet)代替sizeof()内置函数