Use in_array() for compare string condition

Tags: 

PHP

See this code

if($string == 'word1' || $string == 'word2')
{
  // Do Something
} elseif($string == 'word3' || $string == 'word4' || $string == 'word5' || $string == 'word6' || $string == 'word7') {
  // Do Something
}

First if still ok to use but in next elseif is too long! So I tried to find better way to compare many string and in_array() function is the answer.

if(in_array($string, array('word1', 'word2')))
{
  // Do Something
} elseif(in_array($string, array('word3', 'word4', 'word5', 'word6', 'word7'))) {
  // Do Something
}