nodeinto

PHP可变函数参数用法分析
function sum(...$numbers) { $acc = 0; foreach( $numbe...
扫描右侧二维码阅读全文
22
2018/09

PHP可变函数参数用法分析

PHP5.6更新中新增加的可变函数参数。

function sum(...$numbers)
{
  $acc = 0;
  foreach( $numbers as $number )
  {
    $acc += $number;
  }
  return $acc;
}
echo sum(1,2,3,4);

函数参数可以使用三个点省略。传递的参数在函数方法中以数组的形式操作

function sumNumber($a,$b )
{
  echo $a + $b;
  echo "\n";
}
sumNumber(...[1,2]);
$a = [1,2];
sumNumber(...$a);

调用时可以使用三个点加数组的方式,不影响正常使用!

Last modification:September 25th, 2018 at 06:21 pm
If you think my article is useful to you, please feel free to appreciate

Leave a Comment