我试图从一行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;
}
我有点卡在这个部分,并不太确定还有什么可以写这个工作。有什么建议吗?
答案 0 :(得分:10)
使用range
和array_walk
:
function e($s) { echo $s; }
array_walk(range('A', 'Z'), 'e');
答案 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()内置函数