Comparison between a numeric string and an alphanumeric string

Asked

Viewed 759 times

13

The following operation returns true:

var_dump("1" == "1e0"); //1 == 1 -> true

But if I add an "e" at the end of the second string, it already returns false:

var_dump("1" == "1e0e"); //1 == 1 -> false???

If you do the following operation, returns 2:

echo (int)"1" + (int)"1e0e"; // 2

My question is, why the second operation returns false if in the third operation, I am converting the values to integer and returns 1 + 1, ie in the second operation the comparison should be 1 == 1, would not have to return true?

Another question is, if I am comparing 2 strings, why does the first operation return true if they are two different strings?

3 answers

15


1e0 is the scientific notation for 1 i.e 1 vezes 10 elevado a 0. And the php converts numeric strings in numbers for comparison.

var_dump("1000" == "1e3"); //1000 == 1000 -> true 
var_dump("1" == "1e0"); //1 == 1 -> true 

1e0e is not a valid notation therefore:

var_dump("1" == "1e0e"); //1 != da string "1e0e"

Whenever you do casting you will see that this occurs, as casting forces conversion into integer:

var_dump((int)"1" == (int)"1e0e"); // true
  • 4

    This is one of the many drawbacks of PHP. This "achism", this "intrusion" is a major cause of difficult to debug bugs.

  • So, why in the var_dump function the string "1e0" is converted to numeric but the "1e0e" is not? It should not convert to 1 as it does at first without the need of (int) before the string?

  • @Felipe because as already said in the reply 1e0e is not a notation valid scientific.

  • 1

    @Brunoaugusto I agree, if I’m comparing Apples to Oranges, it’s not because both are fruits that I want them to return true.

  • Blessed be the polymorphism provided by interfaces \the/

8

To avoid confusion at the time of comparison use === instead of ==, so beyond the content PHP will compare the type too.

var_dump("1" === "1e0"); // false

String "1" is different from string "1e0"

var_dump("1" === "1e0e"); // false

String "1" is different from string "1e0e"

var_dump((int)"1" == (int)"1e0e"); // true

Whole 1 is equal to the whole 1

3

Compare === does not solve, see that 0xFA is 250, so ( 250 === 0xFA ) results in TRUE

1) Compare the size of a string to an input that is: 0xFA

if( $string <= 255 ) // 250 < 255 // Logo retornará TRUE


2) Convert to string causes the same effect:

( (string)255 === (string)0xFF )

Your example:

Here you have a simple string comparison, 1e0e does not take integer value and 1 == 1 is not the operation that occurs.

var_dump("1" == "1e0e"); //1 == 1 -> false???


Here you have 1e0e as a string and when it converts INT, it naturally assumes the value of 1, which added with 1 results in 2;

echo (int)"1" + (int)"1e0e"; // 2

If you want to compare effectively, you will have to make a combination of checks:
. You can force input to assume a string value with (string)$input
. Combine mb_strlen to compare the length of entries.

If you can, more information on the type of check.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.