PHP program print A to Z alphabets

In this tutorial, we’re going to write a PHP program to print alphabets from A to Z. It’s quite simple code using though PHP range() Function.

PHP range() Function

The range() function creates an array containing a range of elements. This function returns an array of elements from low to high.

Syntax

range(low,high,step)

PHP program print alphabets from A to Z

In the below code first we are going to set the range between A to Z, then looping through each alphabet using foreach() loop.

<?php
foreach (range('A', 'Z') as $alphabet) {
    echo $alphabet." ";
}
?>

Output:

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

PHP program print alphabets from a to z

So we could write a similar program for lowercase characters in a similar way like in below example:

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

Output:

a b c d e f g h i j k l m n o p q r s t u v w x y z

PHP program print alphabets from Z to A in Reverse order

We could print alphabets from Z to A in Reverse order like in below example:

<?php
foreach (range('Z', 'A') as $alphabet) {
    echo $alphabet." ";
}
?>

Output:

Z Y X W V U T S R Q P O N M L K J I H G F E D C B A

Leave A Reply

Your email address will not be published.