KonKarOtMa
KonKarOtMa

Reputation: 3

PHP bug or just a logical flaw

I need to know why, the real logical reason, that the following comparison is executing the "echo" line! It seems to be some kind of sorcery. If you take out the "+" sign, it won't work. If you leave it as it is, it will!

<?php
  $fromUnits = "2";
  $toUnits = "100000000+";
  
  if ($fromUnits >= $toUnits) {
    echo "Bypassed."; // WHY?
  }
?>

Upvotes: 0

Views: 49

Answers (1)

David
David

Reputation: 218837

If the two string values are able to be coerced into numeric values, they are compared as numbers. And the number 100000000 is indeed greater than tha number 2.

However, if both strings can't be coerced into numbers, they are compared as strings. And the string "2" is "greater than" the string "100000000+", in terms of being sorted alphabetically.

Upvotes: 2

Related Questions