学习路径
/
PHP
PHP
/
教学大纲
/
可变长度参数
可变

可变长度参数 属于 PHP

1 个练习

关于 可变长度参数

函数的实参可以这样定义:让它接受任意数量的实参:

<?php

function actOnItems(...$items) {
  // $items is an array containing 0 or more values
  // used to call the function.
}

actOnItems(1, 2, 3, 4); // $items => [1, 2, 3, 4]

如果要在其他实参之外再指定一个可变长度实参,它必须是最后一个实参。

<?php

function writeLines($file_handle, ...$lines)
{
    put_file_contents($file_handle, implode("\n", $lines));
}

writeLines('file.txt', "a", "b", "c");
通过 GitHub 编辑 该链接会在新窗口或标签页中打开

学习 可变长度参数