gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 11:54:33 24 4
gpt4 key购买 nike

在我的服务器上安装 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com