douxuanyi2813 2012-10-05 04:13
浏览 16
已采纳

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

Can the relational operator === (used for identical) be used interchangeably with the != operator" and get the same results? Or will I eventually run into issues later down the road when I do larger programs?

I know I will get the same results in the example below, will this always be true?

//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';
        }
    ?>
  • 写回答

2条回答 默认 最新

  • drfb52000 2012-10-05 04:15
    关注

    Short answer is, no, you can't interchange them because they check for different things. They are not equivalent operators.

    You'll want to use !==

    It basically means both values being compared must be of the same type.

    When you use ==, the values being compared are typecast if needed.

    As you know, === checks the types also.

    When you use !=, the values are also typecast, whereas !== checks the values and type, strictly.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?