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

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

问题描述

确定我完全忘了如何跳过PHP中的参数。

OK I totally forgot how to skip arguments in PHP.

可以说我有:

Lets say I have:

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天全站免登陆