开发者

Can a PHP function accept an unlimited number of parameters? [duplicate]

This question already has answers here: PHP function with unlimited number of parameters (8 answers开发者_如何转开发) Closed 1 year ago.

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?


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', '.');
?>

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', '.'));
?>


You can use these functions from within your function scope:

  • func_get_arg()
  • func_get_args()
  • func_num_args()

Some examples:

foreach (func_get_args() as $arg)
{
    // ...
}

for ($i = 0, $total = func_num_args(); $i < $total; $i++)
{
    $arg = func_get_arg($i);
}


if you have PHP 5.6+ , then you can use

function sum(...$numbers) 

functions.variable-arg-list


You will have 3 functions at your disposal to work with this. Have the function declaration like:

function foo()
{
    /* Code here */
}

Functions you can use are as follows

func_num_args() Which returns the amount of arguments that have been passed to the array

func_get_arg($index) Which returns the value of the argument at the specified index

func_get_args() Which returns an array of arguments provided.


You can use func_get_args() inside your function to parse any number of passed parameters.


If you are using PHP 5.6 or later version, argument lists may include the ... token to denote that the function accepts a variable number of arguments. The arguments will be passed into the given variable as an array; Simply Using ... you can access unlimited variable arguments.

for example:

<?php
function sum(...$numbers) {
    $sum = 0;
    foreach ($numbers as $n) {
        $sum += $n;
    }
    return $sum ;
}

echo sum(1, 2, 3, 4, 5);
?>

If you are using PHP version <= 5.5 then you can access unlimited parameterusing the func_num_args(), func_get_arg(), and func_get_args() functions.


php7 provides an easy way of passing and catching unlimited parameters to a function by using ellipses right before the parameter name in the function definition. Following is an example demonstrating the same:

<?php
function print_names(...$names){
  foreach($names AS $name) echo "The name is: <b>$name</b>.\n";
}
print_names(
  "Superman",
  "Ironman",
  "Spiderman",
  "HULK",
  "Thor",
  "Captain America",
  "Batman"
);

Credits: http://dwellupper.io/post/5/passing-unlimited-parameters-to-functions-in-php7

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜