function foo($x="", $y="", $z=""){
echo $x .'-'. $y .'-'. $z;
}
foo('hello', $z='world');
> hello-world-
I would like to print 'hello--world'. Do I need to specify all arguments between x and z to accomplish that?
function foo($x="", $y="", $z=""){
echo $x .'-'. $y .'-'. $z;
}
foo('hello', $z='world');
> hello-world-
I would like to print 'hello--world'. Do I need to specify all arguments between x and z to accomplish that?
Yes, you have to specify all parameters (arguments) in your function (except the last one!) if it is written like in your example.
One alternative would be to instead pass an array as parameter, but that would not work if the method/function is called from a URL.
You can not specify which parameter to pass when you make the call like in your example:
foo('hello', $z='world');
You are only passing the second parameter in your example.
You would have to make either of the following calls to the function:
foo('hello', 'world');
foo('hello', '', 'world');
I also suggest you another alias:
function foo($x="", $y="", $z=""){
return $x .'-'. $y .'-'. $z;
}
function fooXZ($x="", $z=""){
return foo($x, "", $z);
}
echo fooXZ('hello', 'world');
> hello-world-
and this because if you dont need a parameter, .. .maybe this means you need another function.