PHP 默认函数参数值,如何为“非最后”参数“传递默认值”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10597114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:33:44  来源:igfitidea点击:

PHP Default Function Parameter values, how to 'pass default value' for 'not last' parameters?

phpfunctionparametersdefault

提问by anonymous-one

Most of us know the following syntax:

我们大多数人都知道以下语法:

function funcName($param='value'){
    echo $param;
}
funcName();

Result: "value"

We were wondering how to pass default values for the 'not last' paramater? I know this terminology is way off, but a simple example would be:

我们想知道如何为 'not last' 参数传递默认值?我知道这个术语有点过时,但一个简单的例子是:

function funcName($param1='value1',$param2='value2'){
    echo $param1."\n";
    echo $param2."\n";
}

How do we accomplsh the following:

我们如何完成以下工作:

funcName(---default value of param1---,'non default');

Result:

value1
not default

Hope this makes sense, we want to basically assume default values for the paramaters which are not last.

希望这是有道理的,我们希望基本上假设不是最后的参数的默认值。

Thanks.

谢谢。

回答by MrCode

PHP doesn't support what you're trying to do. The usual solution to this problem is to pass an array of arguments:

PHP 不支持您尝试执行的操作。这个问题的通常解决方案是传递一个参数数组:

function funcName($params = array())
{
    $defaults = array( // the defaults will be overidden if set in $params
        'value1' => '1',
        'value2' => '2',
    );

    $params = array_merge($defaults, $params);

    echo $params['value1'] . ', ' . $params['value2'];
}

Example Usage:

示例用法:

funcName(array('value1' => 'one'));                    // outputs: one, 2
funcName(array('value2' => 'two'));                    // outputs: 1, two
funcName(array('value1' => '1st', 'value2' => '2nd')); // outputs: 1st, 2nd
funcName();                                            // outputs: 1, 2

Using this, all arguments are optional. By passing an array of arguments, anything that is in the array will override the defaults. This is possible through the use of array_merge()which merges two arrays, overriding the first array with any duplicate elements in the second array.

使用这个,所有参数都是可选的。通过传递参数数组,数组中的任何内容都将覆盖默认值。这可以通过使用array_merge()which 合并两个数组来实现,用第二个数组中的任何重复元素覆盖第一个数组。

回答by Jeroen

Unfortunately, this is not possible. To get around this, I would suggest adding the following line to your function:

不幸的是,这是不可能的。为了解决这个问题,我建议将以下行添加到您的函数中:

$param1 = (is_null ($param1) ? 'value1' : $param1);

You can then call it like this:

然后你可以这样称呼它:

funcName (null, 'non default');

Result:

value1
non default

回答by Rohit Pavaskar

The Simple solution of your problem is, instead of using:

您的问题的简单解决方案是,而不是使用:

funcName(---default value of param1---,'non default');

Use the following Syntax:

使用以下语法:

funcName('non default',---default value of param1---);

Example:

例子:

function setHeight2($maxheight,$minheight = 50) {
    echo "The MAX height is : $maxheight <br>";
    echo "The MIN height is : $minheight <br>";
}

setHeight2(350,250);
setHeight2(350); // will use the default value of 50