一个PHP函数可以接受的参数的数量限制? [英] Can a PHP function accept an unlimited number of parameters?

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

问题描述

在PHP中有像功能未设置()支持我们扔他们的任何数量的参数。

In PHP there are functions like unset() that support any number of parameter we throw at them.

我想创建一个类似的功能,能够接受任何数量的参数和处理这一切。

I want to create a similar function that is capable of accepting any number of parameters and process them all.

任何想法,如何做到这一点?

Any idea, how to do this?

推荐答案

在PHP中,使用功能 func_get_args 来获取所有传递的参数。

In PHP, use the function func_get_args to get all passed arguments.

<?php
function myfunc(){
    $args = func_get_args();
    foreach ($args as $arg)
      echo $arg."/n";
}

myfunc('hello', 'world', '.');
?>

另一种方法是一个数组变量传递给你的函数,所以你不要有类似的事情 $ ARG [2]工作; ,而是可以使用的$ args ['MYVAR']; 或rewmember传递什么样的顺序事情,也是无限扩展的,这意味着你可以添加新的变量后,无需更改你已经已经codeD。

An alternative is to pass an array of variables to your function, so you don't have to work with things like $arg[2]; and instead can use $args['myvar']; or rewmember what order things are passed in. It is also infinitely expandable which means you can add new variables later without having to change what you've already coded.

<?php
function myfunc($args){
    while(list($var, $value)=each($args))
      echo $var.' '.$value."/n";
}

myfunc(array('first'=>'hello', 'second'=>'world', '.'));
?>

这篇关于一个PHP函数可以接受的参数的数量限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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