7

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.

2
  • I cannot recreate this on my 5.3.5 install. The strings compare as "not equal" and "not not unequal". Commented Nov 10, 2011 at 16:43
  • I recreated this on PHP 5.2.5 (codepad.org/BJZT1KM1), and PHP 5.3.9-dev (codepad.viper-7.com/MxwYQw). Commented Nov 10, 2011 at 16:48

4 Answers 4

7

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.

Sign up to request clarification or add additional context in comments.

1 Comment

What a strange phenomenon. I was trying to figure this out (obviously in a much more complex script) for hours, and it turns out this was the sole problem. PHP can really bite you in the ass sometimes. This answers my question nonetheless - and it is a good answer. Thanks!
1

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

Comments

1

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. ;-)

1 Comment

OMG this was driving me nuts!!!! I had if($var == "E") { echo "this printed when $var was int(0)"; }
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.