2013-04-02 37 views

回答

9

檢查你的意思是這樣in_array()功能?

if(in_array($example, array($one, $two, $three, $four, $five))) { 
    //code here 
} 
+0

是的,那正是我所需要的。非常感謝你! – totallyuneekname

3

嘗試

$array = array($one, $two, $three, $four, $five); 
$example = 'somestring'; 
if(in_array($example, $array)){ 
    //code here 
} 

,或者如果你想嚴格如=====

if(in_array($example, $array, true)){ 
    //code here 
} 
+0

織補,應該有想過in_array()......那麼明顯 – hurrtz

+0

@hurrtz你應該工作以及它可能在某些情況下更好,但'in_array()'大概是什麼OP是尋找更好對於。 – Class

+0

你好,非常感謝你的回覆。如果你不介意的話,它似乎並沒有認識到if語句是一個實際的語句,因爲它在它後面的「意外的」{'「給出了一個錯誤。也許你知道爲什麼? – totallyuneekname

1
$array = array($one, $two, $three, $four, $five); 

foreach ($array as $value) { 
    if ($example === $value) { 
     something; 
    } 
}