Re: [PHP] Looping from A to Z
Richard K Miller wrote: Good afternoon. I'm having trouble getting PHP to loop from A through Z. Here is what I tried, coming from a C background: for ($l = "A"; $l <= "Z"; $l++) echo $l; I use this: for($i='a'; $i != 'aa'; $i++){ print $i; | -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Looping from A to Z
>>Good afternoon. I'm having trouble getting PHP to loop from A >>through Z. Here is what I tried, coming from a C background: >> >>for ($l = "A"; $l <= "Z"; $l++) >> echo $l; >> >>// Returns A, B, C, ..., X, Y, Z, AA, AB, AC, AD, AE, ... YX, YY, YZ. (26 * 26 results!) >> >>Interestingly, if I make it a "less than" operation instead of "less than or equal to", it works correctly (except for the Z): >> >>for ($l = "A"; $l < "Z"; $l++) >> echo $l; >> >>// Returns A, B, C, ..., W, X, Y (25 results) >> >>Anyone know why PHP would do that? >> >>Richard > >From the PHP 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. -Jim -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Looping from A to Z
Good afternoon. I'm having trouble getting PHP to loop from A through Z. Here is what I tried, coming from a C background: for ($l = "A"; $l <= "Z"; $l++) echo $l; // Returns A, B, C, ..., X, Y, Z, AA, AB, AC, AD, AE, ... YX, YY, YZ. (26 * 26 results!) Interestingly, if I make it a "less than" operation instead of "less than or equal to", it works correctly (except for the Z): for ($l = "A"; $l < "Z"; $l++) echo $l; // Returns A, B, C, ..., W, X, Y (25 results) Anyone know why PHP would do that? Richard I don't see the problem. There are 26 letters. If you say start at A and finish before Z, then you should expect 25 letters returned. Maybe there is something I'm not understanding. tedd -- http://sperling.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Looping from A to Z
Good afternoon. I'm having trouble getting PHP to loop from A through Z. Here is what I tried, coming from a C background: for ($l = "A"; $l <= "Z"; $l++) echo $l; // Returns A, B, C, ..., X, Y, Z, AA, AB, AC, AD, AE, ... YX, YY, YZ. (26 * 26 results!) Interestingly, if I make it a "less than" operation instead of "less than or equal to", it works correctly (except for the Z): for ($l = "A"; $l < "Z"; $l++) echo $l; // Returns A, B, C, ..., W, X, Y (25 results) Anyone know why PHP would do that? Nope... but try... for ( $l = ord("A"); $l <= ord("Z"); $l++ ) echo $l; Also maybe check out the range() function. -philip -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Looping from A to Z
Good afternoon. I'm having trouble getting PHP to loop from A through Z. Here is what I tried, coming from a C background: for ($l = "A"; $l <= "Z"; $l++) echo $l; // Returns A, B, C, ..., X, Y, Z, AA, AB, AC, AD, AE, ... YX, YY, YZ. (26 * 26 results!) Interestingly, if I make it a "less than" operation instead of "less than or equal to", it works correctly (except for the Z): for ($l = "A"; $l < "Z"; $l++) echo $l; // Returns A, B, C, ..., W, X, Y (25 results) Anyone know why PHP would do that? Richard --- Richard K. Miller www.richardkmiller.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php