PHP Alphabet Output

Output the alphabet easily with a simple PHP loop.

Every letter of the English alphabet corresponds to an ASCII code, so rather than manually typing everything out, you can achieve the same result with a simple PHP loop.

The code below will output all uppercase letter from A to Z by running through ASCII codes 65-90 (the values for uppercase letters) and converting each code to its corresponding letter using the in-built PHP function chr().


for ($i=65; $i< =90; $i++) {
 $x = chr($i);
 print $x;
}

For lowercase letters, use the numbers 97-122 instead.

The beauty of letting PHP handle your alphabet output is that you can use this loop to dynamically generate HTML links too.

Below is an example of dynamically generating links to external pages that correspond with each letter:


for ($i=65; $i< =90; $i++) {
 $x = chr($i);
 print "<a href=\"$x.html\">$x</a>";
}