I was just playing around the for statement in php to output the letters "A" to "Z". <?php for ($letters=A; $letters <= Z; $letters++) { print ("$letters, "); } ?> The above code works fine if "$letters <= Y" or any other letter before "Z". But if I use "Z" it goes beyond Z and includes double letters like "AA, AB, AC till YZ. Anyone know how to make it stop at "Z".
I take another route for ($i=1;$i<=26;$i++) { if (!isset($letters)) { $letters=A; } print ("$letters, "); $letters++; } PHP:
Thanks! W'd appreciate if you could let me know how it works in reverse order "Z to A" I tried <?php for ($i=26;$i>=1;$i--) { if (!isset($letters)) { $letters=Z; } print ("$letters, "); $letters--; } ?> PHP: It just prints "Z" 26 times. But the above code works fine with numbers "26 to 1".
you dont have to change the loop to this for ($i=26;$i>=1;$i--) { because we just need the for loop to print 26 alphabet thats all. but its no hurt. decrement? cant seems to find a better way...Maybe you can try this: for ($i=1;$i<=26;$i++) { if (!isset($letters)) { $letters=A; } $string .= ",$letters "; $letters++; } $string = strrev($string); // to test it print $string; PHP: