Skip to content

How to make Alphabet Navigation Menu with PHP?

Last updated on January 17, 2018

In this post, we are going to discuss alphabet navigation, before that do you know How to generate alphabets in a loop with PHP built-in functions, take a look at the below examples.

for ($i=65; $i<=90; $i++) {
 echo chr($i);
// chr returns specific character
//output :ABCDEFGHIJKLMNOPQRSTUVWXYZ
}

In the above example chr() function returns specific character & chr() also accepts negative numbers as an ascii code[char(-100)],although you can generate the above output with php built-in function called range(), see the below example once

foreach(range('A','Z') as $i) {
 echo $i; 
}
//output:ABCDEFGHIJKLMNOPQRSTUVWXYZ

Now I can assume that you got an idea about the alphabet navigation menu from the above examples,we can do this within two steps ,very first what we will do is , will generate alphabets array with any of the above methods then store that results in a variable, in my example $letterList, then loop over all elements of an array, see the complete examples below , you can use any of these methods.

$Letters = range('A','Z');
foreach ($Letters as $litter) {
 echo ''.$litter.'';
//output :ABCDEFGHIJKLMNOPQRSTUVWXYZ
}

Another Method for generating Alphabet Navigation Menu - 2

$letters = array_merge(array('0-9'),range('A','Z'));
foreach ($letters as $letter) {
 echo ''.$letter.'';
}

Another Method for generating Alphabet Navigation Menu - 3

for($i=ord('A');$i<=ord('Z');$i++) {
  echo ''.chr($i).'';
}

The description of the functions used in this tutorial are as follows
chr() this function return a spesific character
range() this function crate an array containing range of elements
foreach() foreach is used to loop over all elements of an array
array_merge() this function merges one or more arrays

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments