Lightning4299
Lightning4299

Reputation: 21

php how to use values from array as function parameters

I have problems using a variable amount of array values as parameters for my function. Since PHP has the functions "func_num_args()" (amount of function parameters) and "func_get_args()" (saves the parameters in an array) for variable amounts of parameters, it would be great to use the values of an array as parameters.

Like a function that adds all integer values:

function add()
{
   $amount = func_num_args();
   $param = func_get_args();

   $sum = 0;


    for ($i=0; $i < $amount; $i++)
    {
       $sum = $sum + $param[$i];
    }
    echo "$sum";
}

Now I would define the array with the values which should be added and run the function:

$values = array(1,2,3,4,5,6,7,24,2345,6323,765,234);

add($values);

But when I run the function as shown, I get an error, since only the first value of the array $values was send over and not all the values.

How do I make sure all values are given as parameters???

If I run the function as follows, it works. But than I need to know the precise amount of parameters which need to be passed on to the function. Here it is just 12, but what to do if the user enters something like 450 parameters ...

add(1,2,3,4,5,6,7,24,2345,6323,765,234)

add($values[0],$values[1], ...)

There is another solution when using "global $values" in the function, where the variable is made available inside the function. But this solutions is not nice and has its problems.

And when running the same function again with global variables I would need to change the function , set to the new parameter set (like $values2).

And again, the functions for "variable amounts of parameters" (func_num_args() and func_get_args() ) would be useless, if you cannot pass variable amounts of parameters to the function by an array.

Help would be appreciated. Greetings

Upvotes: 0

Views: 1301

Answers (3)

kupendra
kupendra

Reputation: 1003

If you don't want to use func_num_args() and func_get_args() then try this :

<?php
function add($values)
{
   $sum=0;
    for ($i=0; $i < count($values); $i++)
    {
      $sum = $sum + $values[$i];
    }
    return $sum;
}
$values = array(1,2,3,4,5,6,7,24,2345,6323,765,234);

echo add($values);
?>

Upvotes: 0

C3roe
C3roe

Reputation: 96306

You are confusing passing an array as (one single) parameter, and accessing multiple parameters as an array inside the function.

foo(1, 2, 3); // three parameters
// vs
foo({1, 2, 3}); // one array as parameter, containing three values

And your function needs the } after the $sum = 0; removed and placed at the very end, then it will work:

function add() {
   $amount = func_num_args();
   $param = func_get_args();

   $sum = 0;

   for ($i=0; $i < $amount; $i++) {
      $sum = $sum + $param[$i];
   }
   return $sum; // functions in general should not “echo out” results,
                // but rather return them
}

echo add(1,2,3); // results in 6

Upvotes: 0

Austin
Austin

Reputation: 3328

You are confusing variable arguments and using an array of values as an argument. In the example you post, $param is is not equal to

array(1,2,3,4,5,6,7,24,2345,6323,765,234)

it is equal to

array(array(1,2,3,4,5,6,7,24,2345,6323,765,234))

that is, you are calling

sum(array(1,2,3,4,5,6,7,24,2345,6323,765,234));

If you want to call

sum(1,2,3,4,5,6,7,24,2345,6323,765,234);

then you can use call_user_func_array:

call_user_func_array('sum', $values);

Upvotes: 2

Related Questions