Martin
Martin

Reputation: 1959

How can two different strings match?

I test the input of a form against values in the data base. When it comes to the fax and phone numbers the string comparison fails.

$fax = trim($args['fax']);
$oldFax = trim($address->getFax());
if($fax != $oldFax){
  $address->setFax($fax);
  $address->flagChanged('fax');
}

The above test finds a match when comparing "+494321" with "00494321". Why is that? If I test using !== instead it works as expected.

Upvotes: 0

Views: 82

Answers (5)

Sverri M. Olsen
Sverri M. Olsen

Reputation: 13283

It is because of type juggling, which means that strings can be used as numbers, and that you are using the "loosely equal to" comparison operator ==.

If an operand of an expression is a number (i.e. contains digits and can be thought of as a number) then it will be converted to the appropriate number type (integer or float) before being compared. So, this is how PHP actually sees the following expression:

("+494321" == "00494321") => (494321 == 494321)

The first operand has the plus sign removed (it just means that the number is positive), and the second operand has the zeros truncated (they do not really mean anything; i.e. 07 and 7 are both the same decimal number, just represented slightly differently).

If you want to compare two strings exactly then there are a few ways to do it. The first is to simply use the "exactly equal to" operator ===. Using this operator, operands will never be type juggled, which is to say that both sides of the comparison operator will be matches exactly; strings will be strings, integers will be integers, and so on.

As an example, these two expressions will both be equal to false:

"123" === 123
"006" === 6

Another way is to use the strcmp function (or similar related functions). It compares strings byte by byte:

$a = "+494321";
$b = "00494321";
if (strcmp($a, $b) === 0) {
    echo "The same";
} else {
    echo "Not the same";
}

The function returns 0 if there are no differences between the two strings (i.e. there are zero differences). If the returned number is lesser than or greater than 0 then the two strings are not the same.

Upvotes: 1

dsd
dsd

Reputation: 551

Because !== compares the value and the type and != implicitly converts the Strings to Integers.

Upvotes: 2

43l0v3k
43l0v3k

Reputation: 337

Let me give you one link:

http://php.net/manual/en/language.operators.comparison.php

If you compare numerical strings, each string is converted to a number ommiting all the leading zeroes and symbols (and the comparison performed numerically).

Upvotes: 1

Karoly Horvath
Karoly Horvath

Reputation: 96258

Whenever possible always do strict comparisons.

== and != do really weird things, for example in your case the both look like numbers, so PHP will compare the strings numerically (= try to convert them to integers and compare them)..

More weird examples in the manual: http://php.net/manual/en/language.operators.comparison.php

Upvotes: 0

chandresh_cool
chandresh_cool

Reputation: 11830

Because

=== and !== 

do strict matching. Whereas

== and !=

does not.

Upvotes: 0

Related Questions