如何在函数调用中跳过可选参数? [英] How would I skip optional arguments in a function call?

查看:25
本文介绍了如何在函数调用中跳过可选参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我完全忘记了如何在 PHP 中跳过参数.

OK I totally forgot how to skip arguments in PHP.

假设我有:

function getData($name, $limit = '50', $page = '1') {
    ...
}

我将如何调用此函数以便中间参数采用默认值(即50")?

How would I call this function so that the middle parameter takes the default value (ie. '50')?

getData('some name', '', '23');

以上是否正确?我似乎无法让它发挥作用.

Would the above be correct? I can't seem to get this to work.

推荐答案

您的帖子是正确的.

不幸的是,如果您需要在参数列表的最后使用可选参数,则必须指定直到最后一个参数为止的所有内容.一般来说,如果你想混合搭配,你给它们默认值 ''null,如果它们是默认值,不要在函数内部使用它们价值.

Unfortunately, if you need to use an optional parameter at the very end of the parameter list, you have to specify everything up until that last parameter. Generally if you want to mix-and-match, you give them default values of '' or null, and don't use them inside the function if they are that default value.

这篇关于如何在函数调用中跳过可选参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆