0

There are lots of examples of false positives when using == to compare two strings in PHP.

For example, '1' == '1.0' is true.

However, are there any false negatives? Is there some string $a such that $a == $a is false due to type juggling?

6
  • 1
    if you want avoid some false positive use strict compare '1' === '1.0' so you compare type and value and not only value Commented Feb 26, 2017 at 20:45
  • Or you can use strcmp() and strcasecmp() to compare string. Commented Feb 26, 2017 at 20:47
  • Is that a typo? Or are you really looking for a string that is not equal to itself? Because no, there is no situation in PHP where $a == $a would ever evaluate to false. Commented Feb 26, 2017 at 20:50
  • php.net/manual/ru/language.operators.comparison.php Commented Feb 26, 2017 at 21:27
  • I'm sorry, but a string comparison from "1.0" == "1" is not true. If you want to compare the values of the strings, you need to convert them to numeric values before or during the test: if (floatval('1') == floatval('1.0')) would be true, as it is comparing the correct types. Read about type juggling: php.net/manual/en/language.types.type-juggling.php Commented Feb 26, 2017 at 21:31

1 Answer 1

1

No, php will not provide false negatives. php will create false positives via type juggling as a feature (it tries to help).

Related questions:

php string comparison unexpected type juggling

Type casting and Comparison with Loose Operator "=="

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

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.