12
votes

I browsed through several similar questions, but they all only state the fact:

If ... comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically.

Okay, I got it. It explains what is going on when '00001' == '1' returns TRUE.

The question is: Why PHP does so?
What is the reason for probing strings for being numeric, and then casting if so? Why can't we just compare two strings already?

I can fairly understand what casting is required if two operands has different types. But why it does "usual math" when both are strings?

3
Cause they didn't take a hint from Perl. :P It would have been so much easier to say like "== compares numerically, eq compares lexically, and === compares strictly". (Though if we had the first two, the latter might never need to be used. Eh, maybe with objects.)cHao

3 Answers

5
votes

You can compare two strings.

"00001" === "1"  // false

Remember == means equivalent === means equal

My only guess as to why this is the case is because in the beginning PHP strived to be a type-less language before they went the route of becoming a loosely typed language.

PHP was originally string processor that allowed for a little scripting inside of it. Since all inputs on the web are textual in nature it had to work hard given textual input to behave sanely when it came to dealing with numbers.

I could be wrong about this but I don't believe the ability to explicitly cast data types entered the stage until PHP 4 with the introduction of the *val functions (like intval) etc. and I think the casting notation, like (int) came around after that.

For the non comparison operations it was pretty easy because they all have a type associated with them (+ - / * all deal with numbers whereas . deals with strings) so a clear path to how things should be cast in those cases is apparent.

But with equality or equivalence checks between variables then the only way to do that was to treat everything that looked like a number as a number because at the time the only way it could be gotten would be by externally would be as a string and there was no way to make it otherwise.

3
votes

Because, PHP produce a product for End-Users, not for Application Developers. So when you produce such product like below:

if (isset($_POST['submit'])){
  if ($_POST['myinput'] == "1") echo 'Yes'; //or == 1
  else echo 'NO';
}

    ?>
    <form name="myform" method="POST" action="">
    <input name="myinput">
    <input name="submit" type="submit">
    </form>

If the user enter 1 or 0001, what do you expect to print in both case? Yes or NO?

the answer is clear. we expect to see Yes. so this is why PHP does so If for any rare reason, we need to definitely compare them, then we should change == to ===

3
votes

Ah, finally I got it. It was quite stupid of me.

Comparison involves not only "is equal" but also "less than" and "greater than". And for the latter two it is obviously critical to cast numerical operands before comparison, because numbers often being represented in PHP as strings, and 11 have to be greater than 9 even if both stored in strings.

So, as compare_function() does all the comparisons at once, returns either 1, 0, -1 to tell if first operand is bigger, equal or less than second respectively - well, it's fairly explains, why operands being cast.