How do i set an optional parameter in PHP after the first optional parameter

断了今生、忘了曾经 提交于 2019-12-07 04:21:30

问题


I'm fairly new to php and I am trying to figure how do I set an optional parameter after the first optional parameter?

For example I have the following code:

function testParam($fruit, $veg='pota',$test='default test'){
echo '<br>$fruit = '.$fruit;
echo '<br>$veg = '.$veg;
echo '<br>Test = '.$test;
}

If i make the following calls:

echo 'with all parama';
testParam('apple','carrot','some string');
//we get:
//with all parama
//$fruit = apple
//$veg = carrot
//Test = some string

echo '<hr> missing veg';
testParam('apple','','something');
//we get:
//missing veg
//$fruit = apple
//$veg = 
//Test = something

echo '<hr> This wont work';
testParam('apple',,'i am set');

I want to try make a call so that in the last example I show 'pota' as the default $veg parameter but pass into $test 'i am set'.

I guess I can pass 0 into $veg then branch it in the code to say if $veg =0 then use 'pota' but just wondered if there's some other syntax as i cant find anything in php.net about it.


回答1:


You can't do what you want with just default parameters. The defaults only apply to missing arguments, and only the last argument(s) can be missing.

You can either add lines like

  $vega = $vega ? $vega : 'carrot';

and call the function as

testParam('apple',false,'something');

or use the more general technique of passing the parameters in an array with the parameter names as keys. Something like

function testparam($parms=false) {
    $default_parms = array('fruit'=>'orange', 'vega'=>'peas', 'starch'=>'bread');
    $parms = array_merge($default_parms, (array) $parms);
    echo '<br>fruit  = $parms[fruit]';
    echo '<br>vega   = $parms[vega]';
    echo '<br>starch = $parms[starch]';
}

testparm('starch'=>'pancakes');
//we get:
//fruit = orange
//vega  = peas
//starch = pancakes

This is a little more verbose but it is also more flexible. You can add parameters and defaults without changing the existing callers.




回答2:


Unfortunately, you cannot do that in PHP.

You have to pass in 0, or null, or some other value and then if the value is 0 or null, change it to the default value.

Here is another question that should give you more information.




回答3:


This is the technique I use:

function testParam($fruit, $veg='pota', $test='default test') {

    /* Check for nulls */
    if (is_null($veg))  { $veg = 'pota'; }
    if (is_null($test)) { $test = 'default test'; }

    /* The rest of your code goes here */
}

Now to use the default value of any optional parameter, just pass NULL like so.

testParam('apple', null, 'some string');

In this example, $veg will equal 'pota'

The downside of this code example is that you have to code the default values twice. You could just as easily set the default value to null in the parameter declaration so you don't have to code the default value twice, but, I like to set it twice because my IDE gives me parameter hinting that instantly shows me the default values in the function signature.



来源:https://stackoverflow.com/questions/1187964/how-do-i-set-an-optional-parameter-in-php-after-the-first-optional-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!