在 PHP 中比较多个值

发布于 2024-09-30 18:16:13 字数 407 浏览 4 评论 0原文

我想从这个:

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

到这个:

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

甜中书 2024-10-07 18:16:13

将值放入数组中,然后使用函数 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

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

$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

双马尾 2024-10-07 18:16:13

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

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

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

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!
}
苄①跕圉湢 2024-10-07 18:16:13
if (in_array($var, array(3, 4, 5, 'string', '2010-05-16'))) {execute code here }

或者,也可以使用开关块:

switch ($var) {
    case 3:
    case 4:
    case 5:
    case 'string':
    case '2010-05-16':
        execute code here;
        break;
}
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;
}
茶色山野 2024-10-07 18:16:13

您可以使用in_array()

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

You can use in_array().

if (in_array($var, array(3,4,5,"string","2010-05-16"))) { .... }
倾城°AllureLove 2024-10-07 18:16:13

或者您可以使用in_array()

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

}

Or you can use in_array()

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

}
另类 2024-10-07 18:16:13

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

在某些情况下,设置一个以值为键的数组,然后使用 isset() 进行检查可能会

更快:

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

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

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

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

In some cases it could be faster to set an array where the values are keys and then check with 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.

小姐丶请自重 2024-10-07 18:16:13
$vals = array (3, 4, 5, 'string', '2010-05-16');
if(in_array($var, $vals)) {
  //execute code here
}
$vals = array (3, 4, 5, 'string', '2010-05-16');
if(in_array($var, $vals)) {
  //execute code here
}
凉城 2024-10-07 18:16:13

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

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

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

然后你可以像这样调用该函数:

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

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文