比较 PHP 中的多个值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4106382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:59:20  来源:igfitidea点击:

Compare multiple values in PHP

phpconditional-operator

提问by John

I'd like to go from this:

我想从这个开始:

if($var == 3 || $var == 4 || $var == 5 || $var =='string' || $var == '2010-05-16') {
   // execute code here
}

to this:

对此:

if($var == (3, 4, 5, 'string', '2010-05-16')) { // execute code here }

Seems very redundant to keep typing $var, and I find that it makes it a bit cumbersome to read. Is there a way in PHP to do simplify it in this way? I read on a post here that when using XQuery you can use the = operator as in $var = (1,2,3,4,5)etc.

继续打字似乎很多余$var,而且我发现阅读起来有点麻烦。PHP中有没有办法以这种方式简化它?我在这里读到一篇文章,当使用 XQuery 时,您可以使用 = 运算符$var = (1,2,3,4,5)等。

回答by Greg

Place the values in an array, then use the function in_array() to check if they exist.

将值放入数组中,然后使用函数 in_array() 检查它们是否存在。

$checkVars = array(3, 4, 5, "string", "2010-05-16");
if(in_array($var, $checkVars)){
    // Value is found.
}

http://uk.php.net/manual/en/function.in-array.php

http://uk.php.net/manual/en/function.in-array.php

回答by NikiC

If you need to perform this check very often and you need good performance, don't use a slow array search but use a fast hash table lookup instead:

如果您需要经常执行此检查并且需要良好的性能,请不要使用慢速数组搜索,而是使用快速哈希表查找:

$vals = array(
    1 => 1,
    2 => 1,
    'Hi' => 1,
);

if (isset($vals[$val])) {
    // go!
}

回答by tdammers

if (in_array($var, array(3, 4, 5, 'string', '2010-05-16'))) {execute code here }

Or, alternatively, a switch block:

或者,或者,一个开关块:

switch ($var) {
    case 3:
    case 4:
    case 5:
    case 'string':
    case '2010-05-16':
        execute code here;
        break;
}

回答by Pekka

You can use in_array().

您可以使用in_array().

if (in_array($var, array(3,4,5,"string","2010-05-16"))) { .... }

回答by MatTheCat

Or you can use in_array()

或者你可以使用 in_array()

if(in_array($var,array(4,5,'string','2010-05-16',true)) {

}

回答by AbcAeffchen

Just to give an alternative solution to the use of in_array:

只是为了提供使用的替代解决方案in_array

In some cases it could be faster to set an array where the values are keys and then check with isset()

在某些情况下,设置值是键的数组可能会更快,然后检查 isset()

Example:

例子:

$checkVars= [3 => true, 
             4 => true, 
             5 => true, 
             "string" => true, 
             "2010-05-16" => true];

if(isset($checkVars[$var])
{
   // code here
}

EDIT: I have done some testing and it looks like this method is faster in most cases.

编辑:我已经做了一些测试,看起来这种方法在大多数情况下更快。

回答by Andrew Sledge

$vals = array (3, 4, 5, 'string', '2010-05-16');
if(in_array($var, $vals)) {
  //execute code here
}

回答by Andrew Sledge

I've had this problem and solved it by making this function:

我遇到了这个问题并通过创建这个函数来解决它:

function orEquals(){
    $args = func_get_args();

    foreach($args as $arg){
        if ($arg != $args[0]){
            if($arg == $args[0]){
                return true;
                break;
            }
        }
    }
    unset($args);
}

then you can just call the function like this:

那么你可以像这样调用函数:

if(orEquals($var, 3, 4, 5, 'string', '2010-05-16')){
//your code here
}