Reputation: 7316
Im trying to spit out each letter of the alphabet from an array on a single line, A-Z.
This is what my code looks like so far:
$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;
}
Im kinda stuck at this part and not quite sure what else to write to make this work. Any suggestions?
Upvotes: 2
Views: 1511
Reputation: 13
This might be help full for you
$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];
}
and you can use count($alphabet) instead of sizeof() built-in function
Upvotes: 0
Reputation: 163238
Use range
and array_walk
:
function e($s) { echo $s; }
array_walk(range('A', 'Z'), 'e');
Working example: http://codepad.org/pedjOlY9
Upvotes: 10
Reputation: 265
I am not sure why you need the array... This is why we have the ASCII code. You can do is like this:
for ($i = 65; $i <=90; $i++)
{
echo chr($i) . PHP_EOL;
}
chr() displays the character in the ASCII map - check it here: http://www.danshort.com/ASCIImap/. If you want to do lowercase - just use strtolower() or the numbers between 97-122 instead. PHP_EOL is a built in constant that outputs end of line. You can change it with ."
" if you are doing HTML.
I think range is a little bit longer to be done, but it still works.
Upvotes: 0
Reputation: 991
$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;
}
Upvotes: 1