PHP 中的三个点 (...) 是什么意思? [英] What is the meaning of three dots (...) in PHP?

查看:44
本文介绍了PHP 中的三个点 (...) 是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

While I am installing Magento 2 on my Server, I got an error. After investigating the code and found that there is are three dots (...), which is producing the error. I included the code I found below:

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

推荐答案

...$str 被称为 PHP 中的 splat 运算符.

此功能允许您为函数捕获可变数量的参数,并结合正常"如果您愿意,可以传入参数.举个例子最容易看出来:

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");
// This would print:
// I'D LIKE 6 APPLES

函数声明中的参数列表中有...操作符,它的基本意思是;... 其他所有内容都应该进入 $strings".您可以将 2 个或多个参数传递给该函数,第二个和后续参数将被添加到 $strings 数组中,以备使用.

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 中的三个点 (...) 是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆