How do the PHP equality (== double equals) and ide

2018-12-31 00:02发布

What is the difference between == and ===?

  • How exactly does the loosely == comparison work?
  • How exactly does the strict === comparison work?

What would be some useful examples?

20条回答
零度萤火
2楼-- · 2018-12-31 00:20

In short, === works in the same manner that == does in most other programming languages.

PHP allows you to make comparisons that don't really make sense. Example:

$y = "wauv";
$x = false;
if ($x == $y)
    ...

While this allows for some interesting "shortcuts" you should beware since a function that returns something it shouldn't (like "error" instead of a number) will not get caught, and you will be left wondering what happened.

In PHP, == compares values and performs type conversion if necessary (for instance, the string "12343sdfjskfjds" will become "12343" in an integer comparison). === will compare the value AND type and will return false if the type is not the same.

If you look in the PHP manual, you will see that a lot of functions return "false" if the function fails, but they might return 0 in a successful scenario, which is why they recommend doing "if (function() !== false)" to avoid mistakes.

查看更多
与君花间醉酒
3楼-- · 2018-12-31 00:25

Given x = 5

1) Operator : == is "equal to". x == 8 is false
2) Operator : === is "exactly equal to" (value and type) x === 5 is true, x === "5" is false

查看更多
春风洒进眼中
4楼-- · 2018-12-31 00:26

The operator == casts between two different types if they are different, while the === operator performs a 'typesafe comparison'. That means that it will only return true if both operands have the same type and the same value.

Examples:

1 === 1: true
1 == 1: true
1 === "1": false // 1 is an integer, "1" is a string
1 == "1": true // "1" gets casted to an integer, which is 1
"foo" === "foo": true // both operands are strings and have the same value

Warning: two instances of the same class with equivalent members do NOT match the === operator. Example:

$a = new stdClass();
$a->foo = "bar";
$b = clone $a;
var_dump($a === $b); // bool(false)
查看更多
姐姐魅力值爆表
5楼-- · 2018-12-31 00:27

You would use === to test whether a function or variable is false rather than just equating to false (zero or an empty string).

$needle = 'a';
$haystack = 'abc';
$pos = strpos($haystack, $needle);
if ($pos === false) {
    echo $needle . ' was not found in ' . $haystack;
} else {
    echo $needle . ' was found in ' . $haystack . ' at location ' . $pos;
}

In this case strpos would return 0 which would equate to false in the test

if ($pos == false)

or

if (!$pos)

which is not what you want here.

查看更多
心情的温度
6楼-- · 2018-12-31 00:27

All of the answers so far ignore a dangerous problem with ===. It has been noted in passing, but not stressed, that integer and double are different types, so the following code:

$n = 1000;
$d = $n + 0.0e0;
echo '<br/>'. ( ($n ==  $d)?'equal' :'not equal' );
echo '<br/>'. ( ($n === $d)?'equal' :'not equal' );

gives:

 equal
 not equal

Note that this is NOT a case of a "rounding error". The two numbers are exactly equal down to the last bit, but they have different types.

This is a nasty problem because a program using === can run happily for years if all of the numbers are small enough (where "small enough" depends on the hardware and OS you are running on). However, if by chance, an integer happens to be large enough to be converted to a double, its type is changed "forever" even though a subsequent operation, or many operations, might bring it back to a small integer in value. And, it gets worse. It can spread - double-ness infection can be passed along to anything it touches, one calculation at a time.

In the real world, this is likely to be a problem in programs that handle dates beyond the year 2038, for example. At this time, UNIX timestamps (number of seconds since 1970-01-01 00:00:00 UTC) will require more than 32-bits, so their representation will "magically" switch to double on some systems. Therefore, if you calculate the difference between two times you might end up with a couple of seconds, but as a double, rather than the integer result that occurs in the year 2017.

I think this is much worse than conversions between strings and numbers because it is subtle. I find it easy to keep track of what is a string and what is a number, but keeping track of the number of bits in a number is beyond me.

So, in the above answers there are some nice tables, but no distinction between 1 (as an integer) and 1 (subtle double) and 1.0 (obvious double). Also, advice that you should always use === and never == is not great because === will sometimes fail where == works properly. Also, JavaScript is not equivalent in this regard because it has only one number type (internally it may have different bit-wise representations, but it does not cause problems for ===).

My advice - use neither. You need to write your own comparison function to really fix this mess.

查看更多
只若初见
7楼-- · 2018-12-31 00:29

In simplest terms:

== checks if equivalent (value only)

=== checks if the same (value && type)


Equivalent vs. Same: An Analogy

1 + 1 = 2 + 0 (equivalent)

1 + 1 = 1 + 1 (same)


In PHP:

true == 1 (true - equivalent in value)

true === 1 (false - not the same in value && type)

  • true is boolean
  • 1 is int
查看更多
登录 后发表回答