简体   繁体   English

如果带或(||)参数的语句不适用于in_array方法

[英]If statement with or (||) argument does not work with in_array method

I have this snippet of code 我有这段代码

public function dynamicSlugAction(Request $request, $slug)
{
    $array1 = ["coffee", "milk", "chocolate", "coca-cola"];
    $array2 = ["water", "juice", "tomato-juice", "ice-tea"];
    if (!in_array($slug, $array1) || !in_array($slug, $array2)) {
        throw new \Exception("The var " . strtoupper($slug) . " is not exist with parameter (slug): " . $slug);
    }
}

Even if I write a right value wich exist in array1 or array2 I have the error launched by the throw new \\Exception . 即使我写了一个在array1或array2中都存在的正确值,我也会因throw new \\ Exception引发错误。

If I remove the or clause in the if statement and I write a right value, no error occured but I can't check the second condition. 如果我删除了if语句中的or子句并输入了正确的值,则不会发生任何错误,但是我无法检查第二个条件。

Where am I wrong in my if statement? 我的if陈述在哪里错?

You need to use logical and (&&) not or. 您需要使用逻辑,而(&&)不能或。 You are saying 你是说

If $slug isn't in array1 or isn't in array 2, throw the exception. 如果$ slug不在array1或不在数组2中,则引发异常。 So to not throw the exception the slug value would need to be in BOTH array 1 and array 2. 因此,为避免引发异常,子弹值必须同时在数组1和数组2中。

What you really want (I assume), if that if the value of slug isn't in either array throw the exception, but if it exists in one of the arrays, do nothing and carry on. 您真正想要的(我假设)是,如果slug的值不在两个数组中,则抛出异常,但是如果它存在于一个数组中,则不执行任何操作并继续执行。 So change your if statement to: 因此,将您的if语句更改为:

if (!in_array($slug, $array1) && !in_array($slug, $array2)) {
  throw new \Exception("The var ".strtoupper($slug)." is not exist with parameter (slug): ".$slug);
}

When you wanna check,if 2 conditions are true then use the logical operator of and(&&).The or operator(||) is to check if either one is true.Just have in mind the Boolean Algebra in order not to lose track. 当您要检查时,如果2个条件为真,则使用and(&&)的逻辑运算符。或运算符(||)将检查其中一个是否为真。请记住布尔代数,以免丢失轨迹。

Or: 要么:

statment1=true;
statment2=false;
if(statment1=true||statment2=true){do stuff}//it will run because at least one statment is true

And: 和:

statment1=true;
statment2=false;
if(statment1=true && statment2=true){do stuff}//it wont run because both statments must be true.
if (!in_array($slug, $array1) || !in_array($slug, $array2))

This condition will throw exception if value does not exists in one of arrays. 如果数组之一中不存在value,则此条件将引发异常。 So if your value exists in one array but not in the other, exception will be thrown. 因此,如果您的值存在于一个数组中而不存在于另一个数组中,则将引发异常。

Check out this logical disjunction table on wikipedia: https://en.wikipedia.org/wiki/Truth_table#Logical_disjunction_.28OR.29 在Wikipedia上查看此逻辑分离表: https//en.wikipedia.org/wiki/Truth_table#Logical_disjunction_.28OR.29

You must use and operator: 您必须使用and运算符:

public function dynamicSlugAction(Request $request, $slug)
{
    $array1 = ["coffee", "milk", "chocolate", "coca-cola"];
    $array2 = ["water", "juice", "tomato-juice", "ice-tea"];
    if (!in_array($slug, $array1) and !in_array($slug, $array2)) {
      throw new \Exception("The var ".strtoupper($slug)." is not exist with parameter (slug): ".$slug);
    }
}

如果您的意思是如果$slug存在于任何数组中,那么您不希望引发错误,则应使用&&

if (!in_array($slug, $array1) && !in_array($slug, $array2))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM