0

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 which exist in array1 or array2 I have the error launched by the throw new \Exception.

If I remove the or clause in the if statement and I write a right value, no error occurred but I can't check the second condition.

Where am I wrong in my if statement?

2
  • 5
    Change the OR for an AND Commented Nov 10, 2016 at 11:11
  • Thank you to all , I better understand why my code does not work, such a rookie mistake. I will check all your suggestion to better understand AND OR clause in if statement ! Commented Nov 10, 2016 at 11:34

5 Answers 5

2

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. So to not throw the exception the slug value would need to be in BOTH array 1 and array 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. So change your if statement to:

if (!in_array($slug, $array1) && !in_array($slug, $array2)) {
  throw new \Exception("The var ".strtoupper($slug)." is not exist with parameter (slug): ".$slug);
}
Sign up to request clarification or add additional context in comments.

Comments

2

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.

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.

Comments

1
if (!in_array($slug, $array1) || !in_array($slug, $array2))

This condition will throw exception if value does not exists in one of arrays. 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

Comments

1

You must use and operator:

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);
    }
}

Comments

1

If you mean if $slug exist in any of the array then you don't want to throw error then you should use &&

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.