php - 如何告诉 PHP 在函数调用中使用默认参数?

标签 php function option-type

<分区>

我有一段代码让我们说:

function nums($a = 1, $b = 2, $c){
    echo "$a, $b, $c";
}

nums(?, ?, 3);

我能以某种方式替换 ? 来告诉 PHP 使用这些参数的默认值,并且只传递一个需要的参数,还是仅通过放置$c 在参数列表中,然后是可选参数?

最佳答案

将默认参数放在最后,然后在函数调用中不要填写参数。

例子:

function nums($c, $a = 1, $b = 2){
    echo "$a, $b, $c";
}

nums(3);

可以通过将默认参数添加到函数调用中来覆盖默认参数:

function nums($c, $a = 1, $b = 2){
    echo "$a, $b, $c";
}

nums(3, 12, 27);

关于php - 如何告诉 PHP 在函数调用中使用默认参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29421177/

相关文章:

swift - 如何在 UiTextLabel 中将可选字符串设置为文本?

php - 不依赖服务器时间获取全局UTC时间

function - 错误 : query has no destination for result data while using a cursor

arrays - 将脚本参数传递给Powershell中的函数

快速增量 Int!不工作

c - 获取可选参数?

php - 在不知道当前位置的情况下查找上一个/下一个 ID(PHP 和 MySQL)

php - 在 MySQL 数据库中存储纬度和经度值的问题

php - Laravel-SQLSTATE[23000] : Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

c++ - 函数指针作为映射参数时出错? C++