php - If statement with or (||) argument does not work with in_array method -
i have 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) . " not exist parameter (slug): " . $slug); } }
even if write right value wich exist in array1 or array2 have error launched throw new \exception.
if remove or clause in if statement , write right value, no error occured can't check second condition.
where wrong in if statement?
you need use logical , (&&) not or. saying
if $slug isn't in array1 or isn't in array 2, throw exception. not throw exception slug value need in both array 1 , array 2.
what want (i assume), if if value of slug isn't in either array throw exception, if exists in 1 of arrays, nothing , carry on. change if statement to:
if (!in_array($slug, $array1) && !in_array($slug, $array2)) { throw new \exception("the var ".strtoupper($slug)." not exist parameter (slug): ".$slug); }
Comments
Post a Comment