Entry
How do I create a string with A-Z in it. Like "ABC....XYZ"? Like $x=[A-Z]; or something?
Jul 3rd, 2003 07:13
krys pryr, Philip Olson, Ben Udall, Phil S.,
There is no shortcut like that in PHP so either define it yourself:
$letters = 'abcdefghijklmnopqrstuvwxyz';
Or do it dynamically, here's one way:
$letter = 'a'; $i = 0;
while ($i++ < 26) {
print $letter++;
}
Or with PHP 4.1.0+, range() also takes on letters although this
will create an array, not a string (see also implode()):
$letters = range('a','z');
Also, chr() and ord() are available to play with:
$x = "";
for ($i = ord('a'); $i <= ord('z'); ++$i) {
$x .= char($i);
}
One thing that _may_ be helpful is to understand the following:
$str = 'abc';
print $str{0}; // a
print $str{2}; // c
Even easier is this:
// lowercase
for ($i = 99; $i <= 122; $i++) {
$alphabet[] = chr($i);
}
//uppercase
for ($i = 65; $i <= 90; $i++) {
$alphabet[] = chr($i);
}