如何使用许多"或"来优化if请求?运营商?

时间:2016-12-08 11:03:23

标签: php if-statement

如果我有很多" ands"有没有办法减少代码?和" ands"在我的if请求相同的值?我只需要$row['animal']一次:

  if ($row['animal'] == "cat") or 
     ($row['animal'] == "dog") or 
     ($row['animal'] == "bird") or 
     ($row['animal'] == "monkey") or 
     ($row['animal'] == "mouse") or 
     ($row['animal'] == "hedgehog"){
     echo "many animals";}

1 个答案:

答案 0 :(得分:0)

echo in_array($row['animal'], array('cat', 'dog', 'bird', 'monkey', 'mouse','hedgehog'))
      ? 'many animals' 
      : 'no animals';