开发者

Can a function take an undefined number of arguments?

开发者 https://www.devze.com 2022-12-28 18:54 出处:网络
Is it possible to hav开发者_如何学JAVAe a function in PHP which takes 1 or more numbers and returns their sum ?You can make use of the function func_num_args and func_get_arg as:

Is it possible to hav开发者_如何学JAVAe a function in PHP which takes 1 or more numbers and returns their sum ?


You can make use of the function func_num_args and func_get_arg as:

function sum() {
        for($i=0,$sum=0;$i<func_num_args();$i++) {
                $sum += func_get_arg($i);
        }
        return $sum;
}

And call the function as:

echo sum(1);       // prints 1
echo sum(1,2);     // prints 3
echo sum(1,2,3);   // prints 6


Yes, it is

function sum() {
    $args = func_get_args();
    return array_sum($args);
}

echo sum(1, 2, 3, 4);

You can also further abstract this notion to be able to wrap any array function

function mapargs() {
    $args = func_get_args();
    return call_user_func(array_shift($args), $args);
}

echo mapargs('array_sum', 1, 2, 3, 4);

or make a currified varargs mapper-wrapper out of it

function mapify($func) {
    return function() use($func) {
        $a = func_get_args();
        return call_user_func($func, $a);
    };
}

$sum = mapify('array_sum');
echo $sum(1, 2, 3, 4);

hope this helps!


Take a look at func_get_args( ). It returns an array containing all arguments passed to the function, basically exactly what you want.

You can use func_num_args( ) to directly receive the number of the arguments.

However note that it is, in most cases, better to just pass an array to the function instead. The above functions are thought for special cases where it is easier and/or more readable to not create and pass arrays directly to the function.

This trivial, rather unrealistic, example shows the advantages:

// By passing an array to the function
function add1( $nums )
{
 $res = 0;
 foreach( $nums as $n )
 {
        $res += $n;
 }
 return $res;
}
add1( array( 1, 2, 3, 4 ) );

// Using func_get_args()
function add2( )
{
 $res = 0;
 foreach( func_get_args() as $n )
 {
        $res += $n;
 }
 return $res;
}
add2( 1, 2, 3, 4 );

add2() is clearly easier to use and the calls to it are more readable in this case.

The solution above is general on how to get a function to take an arbitrary amount of arguments. If your problem is really only about adding up some numbers, use array_add() as the other answers pointed out.


just use array_sum():

$sum = array_sum($arrayOfValues);


what about just passing array?


Yes. We use arrays for that. http://php.net/manual/en/language.types.array.php

And also php has a array_sum function too: http://php.net/manual/en/function.array-sum.php

<?php
$a = array(2, 4, 6, 8);
echo "sum(a) = " . array_sum($a) . "\n";
?>


In PHP 5.6 and later you will be able to do it this way:

function sum(...$nums) {
  for($i=0,$sum=0;$i<count($nums);$i++) {
    $sum += $nums[$i];
  }
  return $sum;
}

Or, simplified using foreach:

function sum(...$nums) {
  $sum=0;
  foreach($nums as $num) {
    $sum += $num;
  }
  return $sum;
}

Source: PHP Manual: Function arguments


Yes.

<?php
function sum(){
  $args = func_get_args();
  $sum = 0;
  foreach($args as $arg){
    if(is_numeric($arg)){
      $sum += $arg;
    }
  }
  return $sum;
}

echo sum().'<br/>
'.sum(1,2,3,"4", "5.63");


function sum($args) {
  $total = 0;
  foreach($args as $num) {
    $total += $num;
  }
  return $total;
}

$args is an array, which you might create like this:

$args = array(3, 4, 5, 6);


Functional way:

function fold($step, $acc, $nums) {
  $val = array_pop($nums);
  if (isset($val)) {
    $acc = call_user_func($step, $val, $acc);
    return fold($step, $acc, $nums);
  } else {
    return $acc;
  }
}

function add($a, $b) {
  return $a + $b;
}

function sum($args) {
  return fold('add',0,$args);
}
0

精彩评论

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

关注公众号