Jump to content

The || trap


keldorn

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/181939-the-trap/
Share on other sites

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
}

 

Link to comment
https://forums.phpfreaks.com/topic/181939-the-trap/#findComment-959660
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.