php - PHP中三个点(...)是什么意思?

标签 php syntax operators

在我的服务器上安装 Magento 2 时,出现错误。查了代码,发现有三个点(...),就是报错。我包含了我在下面找到的代码:

return new $type(...array_values($args));

这个操作符叫什么,它的用途是什么?

最佳答案

这实际上被称为 ... operator in PHP , 但在其他语言中称为 splat 运算符。来自 a 2014 LornaJane blog post关于功能:

This feature allows you to capture a variable number of arguments to a function, combined with "normal" arguments passed in if you like. It's easiest to see with an example:

function concatenate($transform, ...$strings) {
  $string = '';
  foreach($strings as $piece) {
      $string .= $piece;
  }
  return($transform($string));    }

echo concatenate("strtoupper", "I'd ", "like ", 4 + 2, " apples");

(这将打印 I'D LIKE 6 APPLES)

The parameters list in the function declaration has the ... operator in it, and it basically means " ... and everything else should go into $strings". You can pass 2 or more arguments into this function and the second and subsequent ones will be added to the $strings array, ready to be used.

关于php - PHP中三个点(...)是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41124015/

相关文章:

php - 如何将表克隆到另一个数据库?

php - 检查 PDO 查询是否返回结果

php - 未显示唯一编号

c++ - 使用 "and"运算符与 && 运算符有什么缺点吗?

operators - 如何 optional 地提供 OCaml (let*) 运算符以与新旧编译器一起使用?

php - 在mysql中将camelcase表和列名转换为snake case

C# 语法快捷方式

python - 为什么使用 *args 语法的参数列表中的尾随逗号是 SyntaxError?

parsing - 俄语的现代依存解析器

c++ - 没有映射值分配给键时调用 operator [] 的行为