1

I am trying to check if the user is coming from outside the Netherlands or Belgium by using the following 'code':

$countrycode = 'NL'; 

if ($countrycode !== 'NL' || $countrycode !== 'BE') {
echo 'you are not from NL or BE';
} else {
echo 'you are from: ' . $countrycode;
}

I cannot figure out why it's echoing the first occurrence.

PS: I know I could just switch it by using === instead but I kinda wish to know why or what I am doing wrong.

Thanks

Niels
  • 989
  • 1
  • 7
  • 17

2 Answers2

8

That should be AND not OR.

$countrycode = 'NL'; 

if ($countrycode !== 'NL' || $countrycode !== 'BE') { // Since 'NL' !== 'BE' this is true
    echo 'not from NL or BE';
} else {
    echo 'you are from: ' . $countrycode;
}

Your first line should be:

if ($countrycode !== 'NL' && $countrycode !== 'BE') {
kchason
  • 2,686
  • 18
  • 23
  • 1
    ah yea that makes sense. thanks for your quick response! (I can accept your answer in 3 minutes) – Niels Nov 06 '17 at 14:30
0

Try the method strcmp(): http://php.net/manual/de/function.strcmp.php Just keep in mind that it returns an integer instead of boolean. Equality of strings is indicated with a 0 as return value. Also the comparison is case-sensitive.

Alexander Tepe
  • 148
  • 1
  • 1
  • 10