Php writing a function with unknown parameters?

后端 未结 4 1715
终归单人心
终归单人心 2020-12-20 17:07

How would I go about writing a function in php with an unknown number of parameters, for example

function echoData (parameter1, parameter2,) {
    //do somet         


        
4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-20 17:48

    Just for those who found this thread on Google.

    In PHP 5.6 and above you can use ... to specify the unknown number of parameters:

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

    $numbers is an array of arguments.

提交回复
热议问题