Open In App

How to get all alphabetic chars in an array in PHP ?

Improve
Improve
Like Article
Like
Save
Share
Report

All alphabetic characters in an array can be achieved by using chr(), range() with for and foreach loop in PHP. To display the array elements as output we can use echo, print_r() and var_dump() function.

Using range() function: This function is used to create an array of elements of any kind such as integer, alphabets within a given range (from low to high) i.e, the first element of list is considered as low and last one is considered as high. It returns an array of alphabets if the range from A to Z i.e. range(A, Z).

Syntax:

array range( mixed first, mixed second, number steps )

Example 1: Below example illustrate how to display an array of all alphabetic character using range() function.




<?php 
  
// Loop to take values from given range
// and display the range elements
foreach( range('A', 'Z') as $elements) {
      
    // Display all alphabetic elements
    // one after another
    echo $elements . ", ";
}
  
?>


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,

Example 2: Below example illustrate how to display an array of all Alphabetic character using range() function along with array_combine.




<?php 
  
// Merge the lower and upper case alphabetic
// characters and store it into an array
$alphachar = array_merge(range('A', 'Z'), range('a', 'z'));
  
// Loop executes upto all array elements
foreach ($alphachar as $element) {
      
    // Display the array elements
    echo $element . " ";
}
  
?>


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

Using chr() function: The chr() function is used to convert an ASCII value to a character. It accepts an ASCII value as a parameter and returns a string representing a character from the specified ASCII value. The ASCII value can be specified in decimal, octal, or hex values. It returns an array of alphabet if the chr() has its ASCII values from 65 to 91 for alphabet.

Syntax:

string chr( <>int $value )

Example 1: Below example illustrate to display an array of all Alphabetic character using ASCII values.




<?php 
  
// Declare an empty array
$array = Array();
  
// Loop to convert ASCII value
// into characters and store
// it into variables
for( $i = 65; $i < 91; $i++) {
        $array[] = chr($i);
}
  
// Loop to display the array elements
foreach( $array as $k => $v) {
      
    // Display the key and its
    // value of an array 
    echo $k . " => " . $v . ", ";
}
  
?>


Output:

0 => A, 1 => B, 2 => C, 3 => D, 4 => E, 5 => F, 6 => G, 7 => H,
8 => I, 9 => J, 10 => K, 11 => L, 12 => M, 13 => N, 14 => O,
15 => P, 16 => Q, 17 => R, 18 => S, 19 => T, 20 => U, 21 => V,
22 => W, 23 => X, 24 => Y, 25 => Z,

Example 2: Below example illustrate to display an array of all Alphabetic character using ASCII values with the help of chr() function.




<?php 
  
// Loop to display the ASCII value and
// its corresponding character
for( $x = 65; $x <= 90; $x++) {
    echo $x . " => " . chr($x) . ", ";
}
  
?>


Output:

65 => A, 66 => B, 67 => C, 68 => D, 69 => E, 70 => F, 71 => G,
72 => H, 73 => I, 74 => J, 75 => K, 76 => L, 77 => M, 78 => N,
79 => O, 80 => P, 81 => Q, 82 => R, 83 => S, 84 => T, 85 => U,
86 => V, 87 => W, 88 => X, 89 => Y, 90 => Z,


Last Updated : 10 Oct, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads