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

1 Answer

0 votes
by (25.6k points)

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";
?>

...