0

If I have a function such as

function foo($a, $b = 2, $c = 3)
{
  return [$a, $b, $c];
}

Then I can omit passing $b and $c since they have default values. But, say, I want to pass something to $c, but not to $b. How can I do it? Obviously I could swap their places in the declaration (function foo($a, $c = 3, $b = 2), but then I wouldn't be able to only pass $b.

My point is that I want to make a function that has arguments with default values, but which ones I want to pass varies. I tried this: print_r(foo(1, $c = 5)), but it results in [1, 5, 3] not in [1, 2, 5].

Fiodor
  • 796
  • 2
  • 7
  • 18

1 Answers1

2

PHP does not support named parameters. You can use only ordered parameters with proper valued in right position

eg

foo( $a, 4 , 3);

or eventually unassigned value became null in related vars

   foo( $a,  null , 3);

result

 esults in [1, null, 3]
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107