可以===和!=可互换使用?

Jor*_*ith 2 php

关系运算符===(用于相同的)是否可以与!=运算符互换使用"并获得相同的结果?或者当我执行更大的程序时,我最终会遇到问题吗?

我知道我会在下面的例子中得到相同的结果,这总是如此吗?

//example 1  
   <?php
        $a = 1; //integer
        $b = '1'; //string
        if ($a === $b) {     
            echo 'Values and types are same'; 
        }
        else {
            echo 'Values and types are not same';
        }
    ?> 

 // example 2
    <?php
        $a = 1; //integer
        $b = '1'; //string
        if ($a != $b) {     
            echo 'Values and types are not same'; 
        }
        else {
            echo 'Values and types are same';
        }
    ?>
Run Code Online (Sandbox Code Playgroud)

Ste*_*rex 6

简短的回答是,不,你不能交换他们,因为他们检查不同的东西.他们不是等同的运营商.

你会想要使用!==

它基本上意味着要比较的两个值必须是相同的类型.

使用==时,如果需要,要比较的值是类型转换.

如您所知,===也检查类型.

使用!=时,值也是类型转换,而!==严格检查值和类型.