8

I want to do this:

function they_said($alice = "", $bob = "", $charlie = "") {
    echo "Alice said '$alice'";
    echo "Bob said '$bob'";
    echo "Charlie said '$charlie'";
}

they_said($charlie => "Where are the cookies!?");

That way I can ignore passing the first 2 arguments and simply pass the one I want.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Alexander Bird
  • 38,679
  • 42
  • 124
  • 159
  • and [Named Arguments in PHP](http://stackoverflow.com/q/6800379) – Pekka Oct 09 '11 at 14:55
  • 1
    I agree that this question is a duplicate of the others. (I didn't find them because they used different words to describe the problem than I did.) – Alexander Bird Oct 09 '11 at 15:16
  • Although this question is IMO better formulated than the one it duplicates. – sumid Nov 09 '12 at 21:51
  • I know its helpful to pass named argument like in Python...In php you can do it like $my_param = true; my_function($my_param); – jsdev Mar 14 '17 at 06:46
  • 1
    I shared a Q/A as a solution https://stackoverflow.com/questions/56415163 – Behnam Jun 02 '19 at 12:46

1 Answers1

10

No, but you can pass an array:

function they_said($persons)
{
  foreach ($persons as $person => $text)
  {
    echo "$person said '$text'";
  }
}
they_said( array('charlie' => 'Where are the cookies!?') );
ComFreek
  • 29,044
  • 18
  • 104
  • 156