Not equal to != and !== in PHP

前端 未结 4 1829
南旧
南旧 2020-11-27 12:46

I\'ve always done this: if ($foo !== $bar)

But I realized that if ($foo != $bar) is correct too.

Double = still works

4条回答
  •  北荒
    北荒 (楼主)
    2020-11-27 13:43

    !== should match the value and data type

    != just match the value ignoring the data type

    $num = '1';
    $num2 = 1;
    
    $num == $num2; // returns true    
    $num === $num2; // returns false because $num is a string and $num2 is an integer
    

提交回复
热议问题