keldorn Posted November 17, 2009 Share Posted November 17, 2009 Here is a logical OR || trap, I wonder how many people fall for this trap and are left scratching their heads? $id = '0'; // Dont proceed if id is 1 or 0 if($id !='0' || $id !='1'){ exit('It\s a trap'); } ?> But making || to && makes it work. Has anyone here made this mistake? lol Quote Link to comment https://forums.phpfreaks.com/topic/181939-the-trap/ Share on other sites More sharing options...
PFMaBiSmAd Posted November 18, 2009 Share Posted November 18, 2009 When you use negative logic in the comparison, the and/or condition needs to be complemented. Which is why using negative logic should generally be avoided as humans don't do well reading or writing negative logic. if($id =='0' || $id =='1'){ // the value was 0 or the value was 1 } else { // the value was not 0 and it was not 1 } if($id !='0' && $id !='1'){ // the value was not 0 and it was not 1 } else { // the value was 0 or the value was 1 } Quote Link to comment https://forums.phpfreaks.com/topic/181939-the-trap/#findComment-959660 Share on other sites More sharing options...
roopurt18 Posted November 18, 2009 Share Posted November 18, 2009 a && b === !a || !b a || b === !a && !b If you can remember that, then you're good to go. Quote Link to comment https://forums.phpfreaks.com/topic/181939-the-trap/#findComment-959675 Share on other sites More sharing options...
Garethp Posted November 18, 2009 Share Posted November 18, 2009 Hahah, I've never fallen into that trap Quote Link to comment https://forums.phpfreaks.com/topic/181939-the-trap/#findComment-959678 Share on other sites More sharing options...
Zane Posted November 18, 2009 Share Posted November 18, 2009 I've been there before, but I think I realized that it was at that point ... I needed to sleep. Quote Link to comment https://forums.phpfreaks.com/topic/181939-the-trap/#findComment-959683 Share on other sites More sharing options...
keldorn Posted November 18, 2009 Author Share Posted November 18, 2009 Here is a video from standford that explains why 2 statments with != and || will be always be true. Quote Link to comment https://forums.phpfreaks.com/topic/181939-the-trap/#findComment-959688 Share on other sites More sharing options...
roopurt18 Posted November 18, 2009 Share Posted November 18, 2009 Here is a video from standford that explains why 2 statments with != and || will be always be true. $x = 1; $y = 1; if( $x != 1 || $x != $y ) { echo 'true'; }else{ echo 'not always!'; } Quote Link to comment https://forums.phpfreaks.com/topic/181939-the-trap/#findComment-959693 Share on other sites More sharing options...
keldorn Posted November 18, 2009 Author Share Posted November 18, 2009 huh? This always returns to the first echo. How is that not always true? Well I dont really understand myself. $id = '0'; // if($id !='0' || $id !='1'){ echo "First echo"; } else { echo "will never happen"; } Quote Link to comment https://forums.phpfreaks.com/topic/181939-the-trap/#findComment-959701 Share on other sites More sharing options...
Alex Posted November 18, 2009 Share Posted November 18, 2009 $id = '0'; $id !='0' || $id !='1' The literal is if $id is not 0 or $id is not 1. Only one of the two conditionals must hold true for the or to evaluate to true completely. So if $id is not 0, it's true, but if $id is not 1 it is also true. Quote Link to comment https://forums.phpfreaks.com/topic/181939-the-trap/#findComment-959705 Share on other sites More sharing options...
roopurt18 Posted November 18, 2009 Share Posted November 18, 2009 2 statments with != and || will be always be true. This statement in and of itself is incorrect. A != B || A != C will always be true only if B != C Quote Link to comment https://forums.phpfreaks.com/topic/181939-the-trap/#findComment-959711 Share on other sites More sharing options...
Daniel0 Posted November 18, 2009 Share Posted November 18, 2009 a && b === !a || !b a || b === !a && !b If you can remember that, then you're good to go. This is called De Morgan's Law for the mathematically interested. It's fairly easy to prove, and other mathematical structures have this property as well. Quote Link to comment https://forums.phpfreaks.com/topic/181939-the-trap/#findComment-959887 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.