Dynamic Display of the Alphabet with PHP

December 23rd, 2008 by Peter Anselmo Leave a reply »

Here’s a neat trick I recently used: Say you want to the display the alphabet on your web page.  The most likely scenario being for paging links to organize a directory of people or businesses. PHP has a chr() function, which displays the ASCII character for any given integer.

Rather than looping through an array with 26 values, or worse yet, typing out 26 lines of code, just loop through the display code 26 times.

<?php
for ($i=65; $i<=90; $i++) {
 echo chr($i);
}
?>

For those not familiar with ASCII mappings, values 65-90 represent the uppercase letters A-Z. Alternately, you could use the values 97-122 for lowercase a-z.  If you wanted to mix the two (say to display uppercase, but use lowercase in the link) just use the strtoupper() or strtolower() functions inside the loop.  Here’s a more applicable sample:

<?php
for ($i=97; $i<=122; $i++) {
 $x = chr($i);
 echo '<a href="memberlist.php?alpha=' . $x . '>' . strtoupper($x) . '</a>';
}
?>

You can see an example of both applied here.

Leave a Reply