IF statement syntax for several OR options?

A

Anonymous

Guest
Suppose I have a variable $name and I want to assign a phrase to $answer if $name contains "word1", "word2", "word3", "word4" etc. Is there a simple way to construct the IF statement without (IF xxx = "word1") OR (IF xxx = "word2") OR (IF xxx = "word3") ? It seems very inelegant to do it that way!

IF (strpos(strtolower($name), "word1")){
$answer = "That's not a real word. ";
}

Thanks in anticipation.
 
OK, I think I've got it:

Code:
<?php

$name = "Here is a sentence containing word3 and word8";

$array = array('word1','word2','word3','word4','word5','word6','word7','word8','word9');

reset($array);

do {
$key = key($array);
$pos = strpos(strtolower($name), $array[$key]);
if ($pos == true) {
$found = true;
}
} while (next($array));

if ($found == true){
echo ("One or more words were found in the name string.\r\n");
}

?>
 
Back
Top