+4 votes
in Programming Languages by (56.5k points)
What is the best way to create an array of alphabets in PHP? Is there any function to do it?

1 Answer

+1 vote
by (348k points)
selected by
 
Best answer

You can use the range() function to generate an array of alphabets.

Here is an example:

<?php

# create an array of alphabets (a-z)
$alpha_array = range('a', 'z');
print_r($alpha_array);

# create an array of alphabets (A-Z)
$alpha_array = range('A', 'Z');
print_r($alpha_array);

?>

You can also use the chr() function to return characters from ASCII values.

E.g.

<?php

# list alphabets (a-z)
for($i=0; $i<26; $i++){
    print(chr(97+$i));
}
echo "\n";
# list alphabets (A-Z)
for($i=0; $i<26; $i++){
    print(chr(65+$i));
}
echo "\n";
?>


...