PHP if/else错误
在下面的PHP中,如果我使用==比较一个变量,那么如果我使用,它将按照我的预期工作=然后我的代码中断,有人能解释或帮助吗PHP if/else错误,php,demorgans-law,Php,Demorgans Law,在下面的PHP中,如果我使用==比较一个变量,那么如果我使用,它将按照我的预期工作=然后我的代码中断,有人能解释或帮助吗 $_GET['p'] = 'home'; // DOES NOT work, it will always return "show JS" regardless to what string I have if ($_GET['p'] != 'home' || $_GET['p'] != 'create.account'){ echo 'show JS'; }el
$_GET['p'] = 'home';
// DOES NOT work, it will always return "show JS" regardless to what string I have
if ($_GET['p'] != 'home' || $_GET['p'] != 'create.account'){
echo 'show JS';
}else{
echo 'do not show JS';
}
// Works as I would expect it to
if ($_GET['p'] == 'home' || $_GET['p'] == 'create.account'){
echo 'do not show JS';
}else{
echo 'show JS';
}
$\u GET['p']不能同时是两个不同的东西。你说int是第一个表达式; p不在家或不创建.account。这总是真的。您应该使用&&而不是||
if ($_GET['p'] != 'home' && $_GET['p'] != 'create.account')
由于条件
A!=B
但是X
不能同时是A
和B
。所以!((X==A)和&(X==B))
和你的(X!=A)| |(X!=B)
总是正确的。你可以通过否定来发现问题
!( A || B ) === (!A && !B)
您给出的解决方案是不可能的,因为这两个语句都不可能为false,因为这意味着该字符串与两个比较的字符串等效。命中else块的唯一方法是如果两者都为false(因为OR语句)。+1由于(false或True)==True,简单地误用了DeMorgan定律()
!( A || B ) === (!A && !B)