Re: When is "z" != "z" ?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



tedd wrote:
But, what brothers me about the routine, is that is DOES print "z" where it is supposed to. In other words, the characters a-z are output before continuing with aa and so on. The operation doesn't end with "z".

Your condition for the loop to continue is $i<="z".

When $i = "y" it will obviously continue because "y" < "z"
When $i = "z" it will obviously continue because "z" = "z"
When $i = "aa" it will obviously continue because "aa" < "z"

It doesn't stop until you get to "z"+something. As in "za" because at that point "za" > "z" so the last thing you see is the one before "za" which would be "yz".

Here's another way to look at it. All characters before "z" are less than "z" -- correct? So, what value are all characters after "z" (i.e., "aa-yz")? They cannot be greater than, nor can they be less than. So, what are they?

But you are not comparing things in the same context here. Strings are naturally compared alphabetically while you are thinking they are compared numerically somehow. Think of sorting a set of words. Would you expect "aa" to sort before or after "z" ?

So yes, like I said, it is a bit of a quirk, but there is no good answer to what "z"++ should be and we certainly don't want to change the default comparison mechanism to not compare strings alphabetically because that would screw up all sorts of stuff including usorts and peoples' expectations. It's just in this case where you have gotten it into your head that incrementing strings is a good thing to do.

You'd be much better off with a range call to quickly generate a range of characters. You could then loop through that character by character. Or use the chr() function to work with character codes instead.

-Rasmus

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux