简单的PHP比较似乎不起作用

Nie*_*els 1 php if-statement

我试图通过使用以下"代码"检查用户是来自荷兰或比利时以外的地方:

$countrycode = 'NL'; 

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

我无法弄清楚为什么它会在第一次出现时回响.

PS:我知道我可以通过改用===来改变它,但我想知道为什么或我做错了什么.

谢谢

kch*_*son 8

这应该是AND而不是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;
}
Run Code Online (Sandbox Code Playgroud)

你的第一行应该是:

if ($countrycode !== 'NL' && $countrycode !== 'BE') {
Run Code Online (Sandbox Code Playgroud)