【发布时间】:2013-03-24 10:09:49
【问题描述】:
在 php 中,我想知道如何检查变量是否等于列表中的任何变量。
我想我可以尝试类似的东西
if( $example == array($one, $two, $three, $four, $five) ) {
//code here
}
这不起作用,有没有类似的方法?否则,最好的方法是什么?
【问题讨论】:
在 php 中,我想知道如何检查变量是否等于列表中的任何变量。
我想我可以尝试类似的东西
if( $example == array($one, $two, $three, $four, $five) ) {
//code here
}
这不起作用,有没有类似的方法?否则,最好的方法是什么?
【问题讨论】:
你的意思是像in_array()这样的函数吗?
if( in_array($example, array($one, $two, $three, $four, $five)) ) {
//code here
}
【讨论】:
试试
$array = array($one, $two, $three, $four, $five);
$example = 'somestring';
if(in_array($example, $array)){
//code here
}
或者如果您想严格检查,例如 === 而不是 ==
if(in_array($example, $array, true)){
//code here
}
【讨论】:
in_array() 可能更适合 OP 正在寻找的东西。
{之前添加一个额外的) 我上面的代码已经更新了
$array = array($one, $two, $three, $four, $five);
foreach ($array as $value) {
if ($example === $value) {
something;
}
}
【讨论】: