PHP if not 语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3376945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP if not statements
提问by Brooke.
This may be the way my server is set up, but I'm banging my head against the wall. I'm trying to say that if $actionhas no value or has a value that is not "add" or "delete" then have an error, else keep running the script. However, I get an error no matter what $actionis.
这可能是我的服务器的设置方式,但我正在用头撞墙。我想说的是,如果$action没有值或值不是“添加”或“删除”,则出现错误,否则继续运行脚本。但是,无论如何我都会收到错误$action。
$action = $_GET['a'];
if((!isset($action)) || ($action != "add" || $action != "delete")){
//header("location:index.php");
echo "error <br>";
}
$actionis being set properly and if run something like if($action =="add")it works. This is on my local host, so it could be a settings issue.
$action设置正确,如果运行类似的东西if($action =="add")。这是在我的本地主机上,因此可能是设置问题。
回答by John Kugelman
Your logic is slightly off. The second ||should be &&:
你的逻辑有点不对劲。第二个||应该是&&:
if ((!isset($action)) || ($action != "add" && $action != "delete"))
You can see why your original line fails by trying out a sample value. Let's say $actionis "delete". Here's how the condition reduces down step by step:
您可以通过尝试样本值来了解原始行失败的原因。让我们说$action是"delete"。以下是条件如何逐步降低:
// $action == "delete"
if ((!isset($action)) || ($action != "add" || $action != "delete"))
if ((!true) || ($action != "add" || $action != "delete"))
if (false || ($action != "add" || $action != "delete"))
if ($action != "add" || $action != "delete")
if (true || $action != "delete")
if (true || false)
if (true)
Oops! The condition just succeeded and printed "error", but it was supposed to fail. In fact, if you think about it, no matter what the value of $actionis, one of the two !=tests will return true. Switch the ||to &&and then the second to last line becomes if (true && false), which properly reduces to if (false).
哎呀!条件刚刚成功并打印“错误”,但它应该失败。其实仔细想想,不管是什么值,$action两个!=测试中的一个都会返回true。切换||到&&,然后倒数第二行变为if (true && false),适当地减少到if (false)。
There is a way to use ||and have the test work, by the way. You have to negate everything else using De Morgan's law, i.e.:
顺便说一下,有一种方法可以使用||并进行测试。您必须使用德摩根定律否定其他所有内容,即:
if ((!isset($action)) || !($action == "add" || $action == "delete"))
You can read that in English as "if action is not (either add or remove), then".
您可以用英语将其读为“如果操作不是(添加或删除),则”。
回答by Jeremy Ruten
No matter what $action is, it will always either not be "add" OR not be "delete", which is why the if condition always passes. What you want is to use && instead of ||:
不管 $action 是什么,它总是不是“add”或不是“delete”,这就是 if 条件总是通过的原因。你想要的是使用 && 而不是 || :
(!isset($action)) || ($action !="add" && $action !="delete"))
回答by fastcodejava
You could also try:
你也可以试试:
if ((!isset($action)) || !($action == "add" || $action == "delete")) {
// Do your stuff
}
回答by Artefacto
You're saying "if it's not set or it's different from add or it's different from delete". You realize that a != x && a != y, with x != yis necessarily falsesince acannot be simultaneously two different values.
你是说“如果它没有设置或者它不同于添加或者它不同于删除”。您意识到a != x && a != y, withx != y是必然的,false因为a不能同时是两个不同的值。
回答by ina
For future reference, you can quickly create a truth table to check if it evaluates the way you want... it's kind of like Sudoku.
为了将来参考,您可以快速创建一个真值表来检查它是否以您想要的方式评估......它有点像数独。
(!isset($action)) && ($action != "add" && $action != "delete"))
Example:
例子:
column 1 is issetaction, column 2 and 3 evaluates !="add","delete" respectively
column 1 is issetaction, column 2 and 3 evaluates !="add","delete" respectively
if($a=add) T && (F && T) => T && F => FALSE
if($a=delete) T && (T && F) => T && F => FALSE
if($a=add) T && (F && T) => T && F => FALSE
if($a=delete) T && (T && F) => T && F => FALSE
if($a=nothing) T && (T && T) => T && T => TRUE
if($a=nothing) T && (T && T) => T && T => TRUE
回答by Amit Sharma
I think this is the best and easiest way to do it:
我认为这是最好和最简单的方法:
if (!(isset($action) && ($action == "add" || $action == "delete")))
回答by Your Common Sense
Not an answer, but just for the sake of code formatting
不是答案,只是为了代码格式
if((isset($_GET['a'])) $action=$_GET['a']; else $action ="";
if(!($action === "add" OR $action === "delete")){
header("location: /index.php");
exit;
}
Note the exit;statement after header(). That's the important thing. header() does not terminate script execution.
请注意exit;header() 之后的语句。这才是最重要的。header() 不会终止脚本执行。

