代码之家  ›  专栏  ›  技术社区  ›  mikl

针对PHP的合并函数?

  •  124
  • mikl  · 技术社区  · 15 年前

    许多编程语言都有一个合并函数(返回第一个非空值, example )遗憾的是,在2009年,PHP没有。

    在PHP本身获得一个合并函数之前,在PHP中实现一个合并函数的好方法是什么?

    9 回复  |  直到 6 年前
        1
  •  180
  •   Kevin    15 年前

    php 5.3中有一个新的操作符,它可以执行以下操作: ?:

    // A
    echo 'A' ?: 'B';
    
    // B
    echo '' ?: 'B';
    
    // B
    echo false ?: 'B';
    
    // B
    echo null ?: 'B';
    

    来源: http://www.php.net/ChangeLog-5.php#5.3.0

        2
  •  60
  •   flori    9 年前

    php 7引入了一个 coalesce operator :

    echo $_GET['doesNotExist'] ?? 'fallback'; // prints 'fallback'
    

    如果之前的值 ?? 不存在或 null 后面的值 ?? 被拿走了。

    上述改进 ?: 接线员是,那个 ?? 还处理未定义的变量,而不引发 E_NOTICE .

        3
  •  28
  •   Will Shaver    15 年前

    在谷歌上首次点击“php-coalesce”。

    function coalesce() {
      $args = func_get_args();
      foreach ($args as $arg) {
        if (!empty($arg)) {
          return $arg;
        }
      }
      return NULL;
    }
    

    http://drupial.com/content/php-coalesce

        4
  •  17
  •   Ethan Kent    14 年前

    我真的喜欢?接线员。不幸的是,它还没有在我的生产环境中实现。所以我用这个等价物:

    function coalesce() {
      return array_shift(array_filter(func_get_args()));
    }
    
        5
  •  9
  •   Brad Koch Daniel Wright    11 年前

    值得注意的是,由于PHP对未倾斜变量和数组索引的处理,任何类型的合并函数的使用都是有限的。我希望能够做到:

    $id = coalesce($_GET['id'], $_SESSION['id'], null);
    

    但在大多数情况下,这会导致php出现错误,并发出e_通知。在使用变量之前测试它是否存在的唯一安全方法是直接在empty()或isset()中使用它。如果知道联合中的所有选项都是初始化的,那么Kevin建议的三元运算符是最佳选项。

        6
  •  6
  •   Peter Bailey    15 年前

    确保您准确地标识了希望此函数如何与某些类型一起工作。PHP有各种类型检查或类似的函数,因此请确保您知道它们是如何工作的。这是对is_null()和empty()的示例比较

    $testData = array(
      'FALSE'   => FALSE
      ,'0'      => 0
      ,'"0"'    => "0"  
      ,'NULL'   => NULL
      ,'array()'=> array()
      ,'new stdClass()' => new stdClass()
      ,'$undef' => $undef
    );
    
    foreach ( $testData as $key => $var )
    {
      echo "$key " . (( empty( $var ) ) ? 'is' : 'is not') . " empty<br>";
      echo "$key " . (( is_null( $var ) ) ? 'is' : 'is not')  . " null<br>";
      echo '<hr>';
    }
    

    如您所见,empty()为所有这些返回true,但is_null()仅为其中2个返回。

        7
  •  2
  •   hakre    6 年前

    我正在扩展发布的答案 Ethan Kent . 该答案将丢弃非空参数,这些参数由于 array_filter ,这不是什么 coalesce 函数通常是这样的。例如:

    echo 42 === coalesce(null, 0, 42) ? 'Oops' : 'Hooray';
    

    哎呀

    为了克服这个问题,需要第二个参数和函数定义。这个 可赎回的 职能部门负责告知 array_filter 是否将当前数组值添加到结果数组中:

    // "callable"
    function not_null($i){
        return !is_null($i);  // strictly non-null, 'isset' possibly not as much
    }
    
    function coalesce(){
        // pass callable to array_filter
        return array_shift(array_filter(func_get_args(), 'not_null'));
    }
    

    如果你能通过就好了 isset 'isset' 作为第二个论点 阵列滤波器 但是没有这样的运气。

        8
  •  0
  •   mikl    15 年前

    我目前正在使用这个,但我想知道是否不能用PHP5中的一些新特性来改进它。

    function coalesce() {
      $args = func_get_args();
      foreach ($args as $arg) {
        if (!empty($arg)) {
        return $arg;
        }
      }
      return $args[0];
    }
    
        9
  •  0
  •   Paulo Freitas    10 年前

    php 5.3+,带闭包:

    function coalesce()
    {
        return array_shift(array_filter(func_get_args(), function ($value) {
            return !is_null($value);
        }));
    }
    

    演示: https://eval.in/187365