PHP默认函数参数值,如何为'not last'参数“传递默认值”?

时间:2021-05-24 11:01:19

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.

谢谢。

3 个解决方案

#1


52  

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()来合并两个数组,用第二个数组中的任何重复元素覆盖第一个数组。

#2


12  

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

#3


1  

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

#1


52  

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()来合并两个数组,用第二个数组中的任何重复元素覆盖第一个数组。

#2


12  

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

#3


1  

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