带有可选参数的PHP函数 [英] PHP Function with Optional Parameters

查看:142
本文介绍了带有可选参数的PHP函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个可以接受10个参数的PHP函数,但只需要2个参数。有时,我想定义第八个参数,但我不想为每个参数输入空字符串,直到达到第八个参数。



一个想法I是传递一个带有参数数组的抽象函数,它将它传递给实函数。

有没有更好的方法来设置函数,所以我可以通过在只有我想要的参数?

解决方案

使函数采取一个参数:一个数组。传递实际参数作为数组中的值。






编辑: Pekka评论中的链接就是对它的总结。


I've written a PHP function that can accepts 10 parameters, but only 2 are required. Sometimes, I want to define the eighth parameter, but I don't want to type in empty strings for each of the parameters until I reach the eighth.

One idea I had was to pass an abstracted function with an array of parameters which passes it along to the real function.

Is there a better way to set up the function so I can pass in only the parameters I want?

解决方案

Make the function take one parameter: an array. Pass in the actual parameters as values in the array.


Edit: the link in Pekka's comment just about sums it up.

这篇关于带有可选参数的PHP函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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