1

I have no idea what this is called, but I've seen PHP supports a cool parameter passing like:

function myFunc($required_value, $optional_value1 = 'default', $optional_value2 = 'default2'){

}

And then I'd be able to do:

myFunc('something', $optional_value2 = 'test');

So 2 questions in regards to this:

  1. What is this technique called (for future references)?
  2. Since what PHP version is it supported?
Eduard Luca
  • 6,514
  • 16
  • 85
  • 137
  • And sorry if this is a dupe, but since I don't know what the technique is called, I didn't really know what to search for. – Eduard Luca Jul 11 '13 at 11:32
  • Voted to close as duplicate. – Eduard Luca Jul 11 '13 at 11:43
  • @EduardLuca your example isn't setting the `$optional_value2` parameter. It's creating a local variable `$optional_value2` and setting its value to `test`, then it's passing `test` as the second argument which is `$optional_value1`, and it leaves the third argument `$optional_value2` as the default (`default2`). Have a look at my duplicate link, it shows how you can have flexible default parameters using an array that aren't dependent on the order. – MrCode Jul 11 '13 at 11:55
  • I realized that later, as I stated in a comment in @Baba's answer. – Eduard Luca Jul 12 '13 at 11:05

4 Answers4

2

Am not sure what you are able to do but php does not support Named Parameters yet

Ruining

myFunc('something', $optional_value2 = 'test');

Does not mean $optional_value2 would become 'test` in the function See Demo

What you are currently implementing is called Default parameters in functions

Baba
  • 94,024
  • 28
  • 166
  • 217
  • 1
    I see. I think I know what I am doing, and why it's working. When passing the parameter, I'm actually assigning a value to a variable, which returns `true`. The `true` value is then passed to the function as the 2nd param. Mystery solved and yet another reason why PHP sucks comparative to Python. – Eduard Luca Jul 11 '13 at 11:42
  • PHP does not suck .. its Awesome if you only know what you are doing ... Same goes for every other language – Baba Jul 11 '13 at 11:43
  • If you want, we can move that discussion to the chat, but why I'm saying that, is because PHP is limited compared to other languages, so you may know what you are doing, but you still get 10 lines of code in PHP, while in other languages it could have been done in 2 lines. – Eduard Luca Jul 11 '13 at 11:46
  • http://chat.stackoverflow.com/rooms/11/php .... you are fee to discus your options – Baba Jul 11 '13 at 11:47
  • This totally sucks vs Python – Íhor Mé Jun 27 '20 at 18:50
2

its called default parameters.

In your exapmple. there is a slight mistake. given your function

function myFunc($required_value, $optional_value1 = 'default', $optional_value2 = 'default2'){
.....
}

you can call this in the following ways:

myFunc('required_value'); //$optional_value1 = 'default', $optional_value2 = 'default2'
myFunc('required_value', 'opt1'); //$optional_value2 = 'default2'
myFunc('required_value', 'opt1', 'op2');

thing to note is that php doesn't support named parameters, so there order is important. therefore, you can't omit middle params. so the following statements would be wrong because you are trying to use named parameters.

myFunc('something', $optional_value2 = 'test');

For more details, see this

CuriousMind
  • 33,537
  • 28
  • 98
  • 137
0

The only think that comes to my mind is to pass arguments via array.

function f($params)
{ 
    if(!isset($params['name']) || !isset($params['adress'])) 
    {
        //trigger error throw exception or whatever
    }
    //do something with params
} 

and calling

f(array('name' => 'asdfasdf', 'adress' => 'adfasdf')); 

then in function you can check if there are enough params to process function.

PHP doesn't have named params like for example C#.

PHP support default values of parameters so you can use it for example:

 function f($name = "John Doe"){echo $name;}

when you call

echo f(); // John Doe will be displayed
echo f("Robert"); //Robert will be displayed
Robert
  • 19,800
  • 5
  • 55
  • 85
0

this is Default Arguments

Important thing to note is that Optional parameters can only be specified in the last , so Required parameters come before the Optional parameters in the function definition e.g.

function myFunc($param1,$optional1= 'string',$optional2= 5) {

}

while calling the function we can call it as

myFunc($param1);
myFunc($param1 , 'new string');
myFunc($param1,'string',10);

but not myFunc($param1,10); skipping the middle argument is not allowed!!! The method you have used to call the func is wrong

In your example. there is a slight mistake. you can't omit middle params. so the following function call

   myFunc('something', $optional_value2 = 'test'); 

is not allowed! Further $optional_value2 = 'test' cannot appear in the function call...simply 'test' should be passed as an argument to the function.

Taz
  • 173
  • 9