php 5.6+ 不定参数的使用

php5.6+ 版本函数可以传入不定数目的参数:

<?php
/**
 * Created by PhpStorm.
 * User binWei
 */


function getSum(...$numbers)
{
    var_dump($numbers);
    $result = 0;
    foreach ($numbers as $n) {
        $result += $n;
    }
    return $result;
}

echo 'result:' . getSum(2, 4, 6, 8);

结果:
file

发布了145 篇原创文章 · 获赞 24 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/xiaobinqt/article/details/102602380