php - 传递给函数的动态变量量

Kal*_*reg 3 php variables function

我正在寻找最优雅的方法来传递可变数量的参数来传递函数.

考虑这样一个功能:

function abc ($x, $y, $l, $m) {
...
}
Run Code Online (Sandbox Code Playgroud)

是否有一种优雅的方式,没有很多"ifs"或变量数组作为一个变量来调用函数:

all ($x, $y, $l, $m) arguments
first three ($x, $y, $l) arguments
first and third ($x, $l) arguments
only second ($y) argument
last two ($l, $m) arguments
Run Code Online (Sandbox Code Playgroud)

请指教.

Alv*_*aro 5

我宁愿使用数组作为参数,并在函数内部检查它以查看您的变量是否存在:

function abc ($data) {

    if(isset($data['x']){
       //whatever
    }

    if(isset($data['y']){
       //whatever
    }

    ...
}
Run Code Online (Sandbox Code Playgroud)

  • +使用数组是大多数框架中常见的方法,用于可选的命名参数.另请参阅[`extract()`](http://php.net/extract). (5认同)