tags:

views:

1272

answers:

6

Is there a way to get all alphabetic chars (A-Z) in an array in PHP so I can loop through them and display them?

+2  A: 

$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')

funny but usable
Click Upvote
A: 
<?php 

$array = Array();
for( $i = 65; $i < 91; $i++){
        $array[] = chr($i);
}

foreach( $array as $k => $v){
        echo "$k $v \n";
}

?>

$ php loop.php 
0 A 
1 B 
2 C 
3 D 
4 E 
5 F 
6 G 
7 H
...
Aaron Maenpaa
You can just do $array[] = chr($i) to append an element
Tom Haigh
That's essentially what `range` does but this is a wider way of doing it.
Ross
You may of been using JavaScript a while because Array() should really be array(). Or, in JS, [] :)
alex
+31  A: 
$alphas = range('A', 'Z');
PEZ
A: 
$array = range('a', 'z');
benlumley
+3  A: 

To get both upper and lower case merge the two ranges:

$alphas = array_merge(range('A', 'Z'), range('a', 'z'));
PEZ
or simply $alphas = range('A', 'Z') + range('a', 'z');
Cassy
+3  A: 

Another way:

$c = 'A';
$chars = array($c);
while ($c < 'Z') $chars[] = ++$c;
Gumbo