How Can I Make a Php Alphabet Loop?

Sarah Berg

A useful feature of many coding languages is the “for loop”. A basic loop in php has for following structure:

for (init; condition; increment)
  code to be executed;


init: Mostly used to set a counter (but can be any code to be executed once at the beginning of the loop)
condition: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, loop ends.
increment: Mostly used to increment a counter (but can be any code to be executed at the end of the iteration)

So basically, all I used this for was to go over a set of numbers, with i=0, and i++ everytime if went through the loop until i was less than ‘x’.  I didn’t realize php can also loop over the alphabet (using foreach).

foreach(range('a','z') as $i) {
    echo $i;

Knowledge shared.

Last updated by on .

What Are Your Thoughts?

Your email address will not be published. Required fields are marked *