【问题标题】:can php functions have keyword arguments? (like python) [duplicate]php函数可以有关键字参数吗? (如python)[重复]
【发布时间】:2011-12-03 23:55:37
【问题描述】:

可能重复:
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!?");

这样我可以忽略传递前 2 个参数而只传递我想要的那个。

这是 python 中的example

【问题讨论】:

  • 以前被问过;答案是否定的。可悲!
  • 我同意这个问题与其他问题重复。 (我没有找到他们,因为他们用不同的词来描述问题。)
  • 虽然这个问题在 IMO 中的表述比它重复的问题要好。
  • 我分享了一个问答作为解决方案stackoverflow.com/questions/56415163

标签: php function arguments


【解决方案1】:

不,但你可以传递一个数组:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-27
    • 2017-09-17
    • 2011-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多