Monday 16 July 2018

List Alphabetical Characters A-Z with PHP

List Alphabetical Characters A-Z with PHP

I wanted to share a really simple way to get all the characters from the alphabet so that they can be easily iterated through. I’ll be using the PHP range() function which, aside from getting characters from the alphabet, can also be used to obtain other ranges, such as numbers.
Let’s begin by looking at how we can get, and loop through, the letters in the alphabet:

  1. $letters = range('A''Z');  
  2. foreach ($letters as $letter)   
  3. {  
  4.     echo $letter;  
  5. }  
The above would list all the characters from A, through to Z. Note that this would only do the uppercase letters. We could do a similar exercise for lowercase characters in a similar way like so:

  1. $letters = range('a''z');  
  2. foreach ($letters as $letter)   
  3. {  
  4.     echo $letter;  
  5. }  

0 comments:

Post a Comment