How Can I Make a Php Alphabet Loop?

December 21, 2012

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;
  }

Parameters:

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.

Stay in Touch!

Subscribe to our newsletter.

Solutions Architecture

browse through our blog articles

Blog Archive