list alphabet using php





I was looking for ways on how to list the alphabet without manually writing each one of them…
I guess I found an answer here http://xqus.com/printing-alphabet-php.

There were actually different methods being suggested. One that came from the author is by using range(). It can also be used with numbers.

range

(PHP 4, PHP 5)

range — Create an array containing a range of elements

Sample below:

Letters





<?php
foreach(range('a', 'z') as $letter) {
    echo $letter;
}
?>

Numbers

<?php
foreach(range(0, 12) as $number) {
    echo $number;
}
?>

<?php
foreach(range(0, 100, 10) as $number) {
    echo $number;
}

And some suggested these:
array_walk(range('a', 'z'), "printf");
For UPPERCASE: for($i=65; $i<91; $i++) { echo chr($i); }

For lowercase: for($i=97; $i<123; $i++) { echo chr($i); }

I haven’t tried any methods yet but I guess I’ll be trying out range() instead :)

This entry was posted in php and tagged , , . Bookmark the permalink.

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>