在用户定义的函数中可以传递多少个参数? [英] How many parameters can be passed in a user defined function?

查看:119
本文介绍了在用户定义的函数中可以传递多少个参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对编程比较陌生,一直在使用 javascript,现在正在尝试学习 php.标题基本上是这样说的.一个用户定义的函数可以传入多少个参数?

I'm relatively new to programming, have been working a lot in javascript, and now trying to learn php. The title basically says it. How many parameters can I pass in a user defined function?

我注意到标准函数倾向于接受不同的数字,所以我想知道是否可以在函数调用中传递无限数量的参数,只要函数对每个参数都做一些事情.

I've noticed standard functions tend to accept different numbers, so I was wondering if I could potentially pass infinite numbers of parameters in a function call, so long as the function does something with each.

例如:

调用函数:

Myfunction(1,4,3,2,6)

被调用的函数:

function Myfunction(numOfApples, numOfBananas, numOfPears, numOfOranges, numOfRasberries){'do something with each of these values'}

我希望得到一般性的理解,我不想做任何具体的事情,所以水果列表只是传递代表不同事物的参数的一个例子.

I'm asking for general understanding, I'm not trying to do anything specific, so the fruit list thing is just an example of passing parameters that represent different things.

推荐答案

我认为可以发送到函数中的参数数量没有限制.但是,在我看来,一旦超过 3 或 4 个,我倾向于像这样发送数组或对象:

I don't believe there is a limit on the number of parameters you can send into a function. However, in my opinion once you get over about 3 or 4 I tend to move towards sending in an array or object like so:

$send_values = array(
    'value1' = 1,
    'value2' = 3,
    'value3' = 2,
    'value4' = 5,
    'value5' = 1,
);

function Myfunction($input) {

}

这样您就不必担心在变量中发送的确切顺序.此外,发送空值要容易得多.您可以简单地使用 isset($input['value2']) 来查看是否发送了值.但是,如果每个都设置为独立变量,则会出现此问题:

This way you don't have to worry about the exact order you are sending in the variables. Plus sending null values is much easier. You can simply use isset($input['value2']) to see if a value was sent. But if each was set as in independent variable you have this issue:

function Myfunction(value1,value2,value3=null,value4=null,value5=null) {

}

在这种情况下,需要发送 value1 和 value2,但 value 3,4 和 5 可以为 null.所以如果你只是发送前两个值,你可以简单地这样做:

In this case value1 and value2 are required to be sent in, but value 3,4 and 5 can be null. So if you are just sending the first two values you can simply do this:

Myfunction(1,2);

但是,假设您需要发送 value1 value2 和 value5,现在您必须确保为 value3 和 value4 发送空值,以便 value5 填充数据而不是 value3:

However lets say you need to send in value1 value2 and value5, now you have to make sure you send null values for value3 and value4, so that value5 gets populated with the data not value3:

Myfunction(1,2,null,null,5);

在我看来,这可能会变成一个集群,并且会让人感到困惑.这完全是个人喜好,这个问题有一些很好的答案:How参数太多太多?

In my opinion this can just turn into a cluster and can get confusing. It is all personal preference, there are some good answers on this question: How many parameters are too many?

这篇关于在用户定义的函数中可以传递多少个参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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