jeudi 10 novembre 2016

if statement with or (||) argument does not work with in_array method

I have this function in my symfony controller:

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 launch by the 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 two condition.

Where am I wrong in my if statement ?

Aucun commentaire:

Enregistrer un commentaire