How to set an optional parameter in PHP after the first optional parameter

I am new to php and I am trying to figure out how to 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 call 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 to make sure that in the last example I show "pota" as the default $ veg parameter, but go into $ test 'i am set'.

I suppose I can pass 0 to $ veg and then pass it to the code to say if $ veg = 0 then use 'pota', but just wondered if there is another syntax since I cannot find what- either in php.net about it.

+4
source share
3 answers

You cannot do what you want, only with default options. By default, they apply only to missing arguments, and only the last arguments may be missing.

You can add lines like

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

and call the function like

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

or use the more general method of passing parameters in an array with parameter names in the form of keys. Sort of

 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 bit more verbose, but it is also more flexible. You can add parameters and default values ​​without changing existing subscribers.

+7
source

Unfortunately, you cannot do this in PHP.

You must pass a value of 0 or null or another 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.

+2
source

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 for any optional parameter, just pass NULL like this.

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

In this example, $veg will be equal to 'pota'

The disadvantage of this code example is that you need to specify default values ​​twice. You could just as easily set the default value to null in the parameter declaration, so you do not need to specify the default value twice, but I like to set it twice because my IDE gives me a parameter hint that instantly shows me the default values ​​in function signature.

0
source

All Articles