Why doesn't this code simply print letters A to Z?
PhpPhp Problem Overview
<?php
for ($i = 'a'; $i <= 'z'; $i++)
echo "$i\n";
This snippet gives the following output (newlines are replaced by spaces):
>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 aa ab ac ad ae af ag ah ai aj ak al am an ao ap aq ar as at au av aw ax ay az ba bb bc bd be bf bg bh bi bj bk bl bm bn bo bp bq br bs bt bu bv bw bx by bz ca cb cc cd ce cf cg ch ci cj ck cl cm cn co cp cq cr cs ct cu cv cw cx cy cz da db dc dd de df dg dh di dj dk dl dm dn do dp dq dr ds dt du dv dw dx dy dz ea eb ec ed ee ef eg eh ei ej ek el em en eo ep eq er es et eu ev ew ex... on to yz
Php Solutions
Solution 1 - Php
From the docs:
>PHP follows Perl's convention when dealing with arithmetic operations on character variables and not C's.
>
>For example, in Perl 'Z'+1
turns into 'AA'
, while in C 'Z'+1
turns into '['
( ord('Z') == 90
, ord('[') == 91
).
>
>Note that character variables can be incremented but not decremented and even so only plain ASCII characters (a-z and A-Z) are supported.
From Comments:-
It should also be noted that <=
is a lexicographical comparison, so 'z'+1 ≤ 'z'
. (Since 'z'+1 = 'aa' ≤ 'z'
. But 'za' ≤ 'z'
is the first time the comparison is false.) Breaking when $i == 'z'
would work, for instance.
Solution 2 - Php
Because once 'z' is reached (and this is a valid result within your range, the $i++ increments it to the next value in sequence), the next value will be 'aa'; and alphabetically, 'aa' is < 'z', so the comparison is never met
for ($i = 'a'; $i != 'aa'; $i++)
echo "$i\n";
Solution 3 - Php
Others answers explain the observed behavior of the posted code. Here is one way to do what you want (and it's cleaner code, IMO):
foreach (range('a', 'z') as $i)
echo "$i\n";
In response to ShreevatsaR's comment/question about the range function: Yes, it produces the "right endpoint", i.e. the values passed to the function are in the range. To illustrate, the output from the above code was:
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
Solution 4 - Php
Others already said why PHP doesn't show what you expect. Here's how you get the result you might want:
<?php
for ($i = ord('a'); $i <= ord('z'); $i++)
echo chr($i);
?>
Solution 5 - Php
Why not just use range('a','z')
?
Solution 6 - Php
Try this code. I think this code will be helpful to you.
$alphas = range('A', 'Z');
foreach($alphas as $value){
echo $value."<br>";
}
Display 26 letters in sequence.
Solution 7 - Php
<?php
$i = 'a';
do {
echo ($j=$i++),"\r\n";
} while (ord($j) < ord($i));
?>
Solution 8 - Php
PHP has the function of looping letters and can exceed beyond single characters; the rest will be done this way: aa ab ac... zz, and so on.
Try this:
<?php
for ($i = 'a'; $i !== 'aa'; $i++)
echo "$i\n";
?>
Solution 9 - Php
Also this can be used:
for ($i = 'a'; $i <= 'z'; $i=chr(ord($i)+1))
echo "$i\n";
Solution 10 - Php
While the above answers are insightful to what's going on, and pretty interesting (I didn't know it would behave like this, and its good to see why.
The easiest fix (although perhaps not the most meaningful) would be just to change the condition to $i != 'z'
<?php
for ($i = 'a'; $i != 'z'; $i++)
echo "$i\n";
?>
Solution 11 - Php
Perhaps this code will work. It’s easy & can be understood:
<?php
$ascii_val = ord("a");
for($i=$ascii_val;$i<$ascii_val+26;$i++){
echo chr($i)."\n";
}
?>
where 26 is the total number of letters in the alphabet.
Solution 12 - Php
The PHP does not consider 'AA' less than 'Z'. The best way to make this is:
for($i = 'a'; $i != 'aa'; $i++) {
echo $i;
}
abcdefghijklmnopqrstuvwxyz
Solution 13 - Php
There are several ways to do that.
1. First you can take the range from 'a' to 'z'. Then iterate a loop over it.
foreach(range('a', 'z') as $i)
{
echo $i . "\n";
}
2. You can print the letters using asci value of the characters.
for($i = 97 ; $i<=122; $i++)
{
echo chr($i) . "\n";
}
3. You can take the asci value of 'a' and run a loop till the asci value of 'z':
for ($x = ord('a'); $x <= ord('z'); $x++)
{
echo chr($x) . "\n";
}
4. For printing a new line in html you can append the
to the end of each characters.
for($i='a';$i<='z';$i++)
{
echo $i. "<br />";
}
Solution 14 - Php
this code will work. It’s easy & can be understood:
<?php
// print form A to ZZ like this
// A B C ... AA AB AC ... ZA .. ZZ
$pre = "";
$i = ord('a');
for ($x = ord('a'); $pre . strtoupper(chr($x-1)) != 'AH'; $x++)
{
echo "". $pre . strtoupper(chr($x)) . "\n";
if(chr($x) === "z"){
$pre = strtoupper(chr($i++));
$x = ord('a');
$x--;
}
}
Solution 15 - Php
Wow I really didn't know about this but its not a big code you can try echo "z" after loop Mark is Absolutely Right I use his method but if you want alternative then this may also you can try
<?php
for ($i = "a"; $i = "y"; $i++) {
echo "$i\n";
if ($i == "z") {}
}
echo "z";
?>