Home > Questions > Why doesn't this code simply print letters A to Z?

Why doesn't this code simply print letters A to Z?

Answered
0
0
<?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

NOTE: This question was originally posted at StackOverflow.com by Milan Babuškov

  • Billy
    Wow the output from that is really unexpected.

    NOTE: This comment was originally posted at StackOverflow.com by GWW

  • Carolyn
    PHP is not C, even if the syntax tries to convince you of the contrary.

    NOTE: This comment was originally posted at StackOverflow.com by joni

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

    NOTE: This comment was originally posted at StackOverflow.com by Surreal Dreams

  • Carolyn
    really fascinating!

    NOTE: This comment was originally posted at StackOverflow.com by joni

  • Kelly
    I mean, what the hell?

    NOTE: This comment was originally posted at StackOverflow.com by Jeffrey

Good Answer
226
0

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.

NOTE: This answer was originally posted at StackOverflow.com by CMS

  • You must to post comments
78
0

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

NOTE: This answer was originally posted at StackOverflow.com by Mark Baker

  • You must to post comments
58
0

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

NOTE: This answer was originally posted at StackOverflow.com by GreenMatt

  • You must to post comments
1
0

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";
            ?>

NOTE: This answer was originally posted at StackOverflow.com by Mohit Bumb

  • You must to post comments
1
0

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";
?>

NOTE: This answer was originally posted at StackOverflow.com by jon_darkstar

  • You must to post comments
1
0

Why not just use range('a','z')?

NOTE: This answer was originally posted at StackOverflow.com by stillstanding

  • You must to post comments
0
0

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";
?>

NOTE: This answer was originally posted at StackOverflow.com by James Dantes

  • You must to post comments
0
0
<?php

$i = 'a';
do {
echo ($j=$i++),"\r\n";
} while (ord($j) < ord($i));

?>

NOTE: This answer was originally posted at StackOverflow.com by Matt H.

  • You must to post comments
0
0

Other’s 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);
?>

NOTE: This answer was originally posted at StackOverflow.com by Filip Ekberg

  • You must to post comments
Showing 9 results
Your Answer
Guest Author
Post as a guest by filling out the fields below or if you already have an account.
Name*
E-mail*
Website
CAPTCHA*
Enter the characters shown on the image.