How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?

前端 未结 11 2156
故里飘歌
故里飘歌 2020-11-22 16:52

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

What would be some useful examples?


Additionally, how are these operators us

11条回答
  •  忘掉有多难
    2020-11-22 17:23

    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.

提交回复
热议问题