php函数有关键字参数吗? (比如python)

时间:2011-10-09 14:53:37

标签: php function arguments

  

可能重复:
  named PHP optional arguments?

我想这样做:

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!?");

这样我可以忽略传递前两个参数并简单地传递我想要的那个。

这是python中的example

1 个答案:

答案 0 :(得分:8)

不,但你可以传递数组:

function they_said($persons)
{
  foreach ($persons as $person => $text)
  {
    echo "$person said '$text'";
  }
}
they_said( array('charlie' => 'Where are the cookies!?') );