Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 945
thiswayup Avatar asked Jul 27 '09 12:07

thiswayup


People also ask

How do you specify an optional parameter?

Optional parameters are defined at the end of the parameter list, after any required parameters. If the caller provides an argument for any one of a succession of optional parameters, it must provide arguments for all preceding optional parameters. Comma-separated gaps in the argument list aren't supported.

How do I make an argument optional in PHP?

Arguments that do not stop the function from working even though there is nothing passed to it are known as optional arguments. Arguments whose presence is completely optional, their value will be taken by the program if provided.

Can echo in PHP accept more than 1 parameter?

Definition and Usage. The echo() function outputs one or more strings. Note: The echo() function is not actually a function, so you are not required to use parentheses with it. However, if you want to pass more than one parameter to echo(), using parentheses will generate a parse error.

How do you pass an optional parameter in go?

Go doesn't support optional parameters , default values and function overloading but you can use some tricks to implement the same. Sharing one example where you can have different number and type of arguments in one function. It's a plain code for easy understanding you need to add error handling and some logic.


1 Answers

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.

like image 90
Lucky Avatar answered Nov 09 '22 09:11

Lucky