Comparison operators with Strings

Hi Guys

The Manual on comparison operators says “PHP Translate strings and resources to numbers…”
But how does it convert into numbers? What makes ‘PHP’ less than ‘php’?

[php]
$P = “PHP”;
$p = “php”;

echo ($P < $p);
[/php]
//returns 1

[php]echo ($P == $p);[/php]
//returns nothing?

It’s simply a matter of precedence. PHP give precedence to all lower case alpha characters over any upper case alpha characters. Since lowercase will always precede upper-case they can never be equal. Additionally, I don’t see you ever in your life needing to do anything like that.

It’s based on the ASCII table…

http://www.asciitable.com/

Upper Case A = 65
Lower Case a = 97

TopCoder, could you please cite a reference that that is what PHP uses to determine precedence on the alpha characters

It’s un-documented in PHP, but it is how it works. You can easily test it using the ASCII table.

It works the same way as it does in Fortran, Cobol, Pascal, ASP, .NET when doing text comparison.

Since there is no documentation that you know of, I will personally refrain from using it as an answer since I cant back it up. All the number correlations in the table do backup what I said about lower case having precedence over uppercase though.

You consider that valid, based on your correlations, but without documentation to back it up?

You can do all the testing you want with the Ascii Chart and you will see that it’s spot on.

Not sure what you mean. I was just saying that when I tested between upper and lower case, that lower case has precedence over all uppercase. Why, I can’t say for sure myself because there is no documentation. All I could tell someone is just that based on testing that is the case. I couldn’t give someone a supported reason as to why.

The correlations I refer to are the values in the ASCII table. They are not my correlations. It is in line with what you said.

Is it by chance documented in the other languages you mentioned?

http://blog.codinghorror.com/sorting-for-humans-natural-sort-order/

You can read more about it in the above link, where they state the default sort order in most programming languages is ASCII based. They want it to be Natural Based, but it’s not. PHP has special built in sorts to sort strings in Natural order, instead the default of ASCII.

http://www.php.net/manual/en/function.natsort.php

Very interesting reading. Learned me a few new words. ASCIIbetical , otherwise known as computer sorting and natural sorting plus a couple of the PHP built in sorting functions that I have never needed to use.

I can now stand solid on what you said and answer someone intelligently on the subject, that it is based on the ASCII table. I know a lot, but I don’t know everything. Thanks TopCoder

Yeah, I never heard the word “ASCIIbetical” before today either. No problem, you know a ton more then I do about PHP. I just have rookie experience in a lot of languages that I’ve taken in high school and college.

Sponsor our Newsletter | Privacy Policy | Terms of Service