Search code examples
phpor-operatorand-operator

Why OR operator always returns even if it meets one of the conditions?


Why OR operator always returns even if $type meets one of the conditions? Logically it should check if $type is not ONE or not TWO. If it is not ONE it should resume.

function my_function(){     
        if( $type !== 'one' || $type !== 'two' ) return;
        ?>

and when I use AND operator it checks if ONE OF THE CONDITIONS MEETS? Logically it should be not ONE AND not TWO to not return.

function my_function(){     
        if( $type !== 'one' && $type !== 'two' ) return;
        ?>

It is blowing my mind)) Please somebody explain me...


Solution

  • Using the OR operator with a negative test can be very confusing. In your OR example, the test will always be true. You are saying "if the value is not one or it is not two". Since it can never be BOTH one and two, the expression is always false.

    The AND operator is what you want here. It says "if it's not one and it's not two". So it it's only true if the value is neither one or two.