Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integer string comparison are equal (PHP bug?)

Tags:

php

I'm comparing two strings like so:

<?php

$Str1 = '111122223333444455556666';
$Str2 = '111122223333444455557777';

if($Str1 != $Str2){
    // Do something
} else {
    // Do something else
}

?>

Obviously, $Str1 is not the same as $Str2, but still always executes the else-block. I know that I should simply use === or !== to compare here, but I'm wondering why (basically) any other value I try does in fact evaluate the way it's expected to.

I also read this in the documentation "If the string does not contain any of the characters '.', 'e', or 'E' and the numeric value fits into integer type limits (as defined by PHP_INT_MAX), the string will be evaluated as an integer.", so I'm guessing it should not be below or the same as the value of PHP_INT_MAX (which is by far less than the strings I'm evaluating above) - assuming that's what they mean by "fits into". So why are the strings above being evaluated as being the same? Could it possibly be a PHP bug or is there something I'm missing?


I'm using PHP version 5.3.8 since yesterday, coming from PHP 5.3.6. Running on Windows XP.

like image 757
user966939 Avatar asked Nov 10 '11 16:11

user966939


People also ask

Can we compare string and integer PHP?

The way PHP converts values when comparing boolean, integers and strings have changed. One of the related advantage is that 0 == "not-a-number" is now false, instead of true. This will prevent some Hash security issues. The original post hints at using only the strict comparison operator, such as === and !==

Can you use == to compare strings in PHP?

The most common way you will see of comparing two strings is simply by using the == operator if the two strings are equal to each other then it returns true. This code will return that the strings match, but what if the strings were not in the same case it will not match.

How does PHP compare strings with comparison operators?

PHP will compare alpha strings using the greater than and less than comparison operators based upon alphabetical order. In the first example, ai comes before i in alphabetical order so the test of > (greater than) is false - earlier in the order is considered 'less than' rather than 'greater than'.

Will Comparison of string 15 and integer 15 works in PHP?

The class doesn't provide a method to convert this type to a number, so it's not able to perform the comparison. However, it appears that this type has a method to convert to string. But PHP won't do a double conversion when performing the comparison.


4 Answers

What is happening here is that the numbers are cast to floats (as they don't fit into ints) and the floats happen to be the same. See the PHP source.

This script shows that the parsed floats indeed have the same value.

like image 73
NikiC Avatar answered Sep 24 '22 11:09

NikiC


That's what it looks like, if I do this:

$Str1 = '111122223333444455556666 '; 
$Str2 = '111122223333444455557777 ';

It comes out fine (note the space)

So it must be converting to number and not seeing the difference because of length

like image 33
kilrizzy Avatar answered Sep 22 '22 11:09

kilrizzy


One could get the thought that PHP is bit relaxed in these conversions?! Then again, do you rather want good old strict type-checking?

Not as advanced as above, but still enough to grab an hour of my time recently.

<? 
$a_var = 0;
if ($a_var=="WHATEVER")
  echo "WATCH OUT! This will be printed!";
//- as "whatever" is converted to an int

if ((string)$a_var=="WHATEVER")
  echo "OK, this will of course not be printed!";

$a_var = "0";
if ($a_var=="WHATEVER")
  echo "OK, this will of course also not be printed!";
?>

Conclusion: BEWARE of the automated casting in PHP. It may play tricks on you now and then, and bugs may be very hard to track. Explicit casting one time too many may be smarter at times, rather than relying on our great PHP understanding. ;-)

like image 21
user3104328 Avatar answered Sep 25 '22 11:09

user3104328


Use

if (strcmp($Str1, $Str2) == 0) {
    //equal
} else {
    //not equal
}

As mentioned in https://docstore.mik.ua/orelly/webprog/php/ch04_06.htm, first compare the two as String and then compares to 0 is there're equal.

like image 20
herbertD Avatar answered Sep 25 '22 11:09

herbertD