[英]PHP multiple If conditions are true
I have this PHP code: 我有这个PHP代码:
$supplier = $line[2];
if ($supplier == "supplier")
{
} else {
Header("Location: premium.php");
}
Now I want that to include $supplier == "demo" to the PHP code to be as follows: 现在,我希望将$ supplier ==“ demo”包含在PHP代码中,如下所示:
$supplier = $line[2];
if ($supplier == "supplier" && $supplier == "demo")
{
} else {
Header("Location: premium.php");
}
but the latter code is not working for either... I mean if the $supplier is equal to something else besides "supplier" or "demo" it is still bypassing it. 但是后者的代码都不起作用。。。我的意思是,如果$ supplier等于“ supplier”或“ demo”之外的其他东西,它仍然会绕过它。 It only worked for the first one.
它仅适用于第一个。
How can I arrange it please? 请问如何安排?
The line if ($supplier == "supplier" && $supplier == "demo")
该行
if ($supplier == "supplier" && $supplier == "demo")
makes no sense. 没有意义。
$supplier
could not have changed in the middle of the conditional statement, thus that will never be true. $supplier
不能在条件语句的中间进行更改,因此永远不会如此。 You might be looking for ||
您可能正在寻找
||
(or). (要么)。
if ($supplier == "supplier" && $supplier == "demo")
can never be true, since the $supplier can never be both at same time. 永远不可能是真实的,因为$ supplier永远不能同时是两个。 You need an OR there like so:
您需要像这样的OR:
if ($supplier == "supplier" || $supplier == "demo")
Definitely take this opportunity to understand the meaning of &&
and ||
一定要利用这个机会来了解
&&
和||
的含义。 as others have mentioned, but if "or" is indeed the logic you're looking for, in this case I'd recommend this: 正如其他人提到的那样,但是如果您确实要寻找“或”逻辑,在这种情况下,我建议您这样做:
if ( in_array( $supplier, array( "supplier", "demo" ) ) )
"&&" means: both connected statements have to be true ! “ &&”表示:两个连接的语句都必须为true! "||"
“||” means: one of the statements have to be true !
意思是:其中一项陈述必须为真!
Your construct doesn't make sense, because you said $supplier has to be "demo" AND "supplier", which can never be true, as a $var can only have one value. 您的构造没有意义,因为您说$ supplier必须是“ demo”和“ supplier”,这永远是不正确的,因为$ var只能有一个值。
WRONG: 错误:
if ($supplier == "supplier" && $supplier == "demo") // AND would also work
RIGHT: 对:
if ($supplier == "supplier" || $supplier == "demo") // OR would also work
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.