我想定义一个函数 doSomething(arg1, arg2)
,默认值为 arg1=val 和 arg2=val
当我写作时
function doSomething($arg1="value1", $arg2="value2"){
// do something
}
现在是否可以使用默认 arg1 和 arg2="new_value2"调用 doSomething
最佳答案
有时如果我有很多默认参数,我会使用一个数组来包含参数并将其与默认值合并。
public function doSomething($requiredArg, $optional = array())
{
$defaults = array(
'arg1' => 'default',
'arg2' -> 'default'
);
$options = array_merge($defaults, $optional);
}
不过只有当你有很多争论时才真正有意义。
关于php - 如何在 php 中使用默认参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2829529/