6

Im looking for a bit of advice, I have visited this page in the manual, however it might be the wrong page or I misinterpreted the instructions as I am still confused.

enter image description here

I have the following question about the assignment above. I would like to know:

  1. 1st and foremost what is a variable-length parameter?
  2. Creating the function is not a problem however, how do I set the number of argumnets, since according to the question (if I understand it correctly) the function should be able to take any number of arguments. I guess it comes back to my 1st question regarding variable-length paramenters again...?

Thank you for reading

3
  • The term is "variable length parameter list". The "list" is the important thing here. It describes a function that can be called (used) with a variable number or parameters. You do not "set" the number of parameter somehow. You simply call the function with whatever list of parameters you want and leave it to the function to sort that out. php offers means to query the list of parameters a function was called with. A good example for such a function is php's sprintf(...). Commented Apr 19, 2016 at 6:56
  • @arkascha thank you, am I correct if I say when I create the function, it will have no parameters i.e. function nrs() Commented Apr 19, 2016 at 6:58
  • Sure, you certainly can do that. Though often you want to the inital parameter(s) to be defined in an explicit manner. That has two advantages: 1. you can use type hinting for those first parameters which is a good thing for special, controlling parameters and 2. you enable modern IDEs to assist in a more constructive way. Commented Apr 19, 2016 at 7:25

6 Answers 6

5

PHP has support for variable-length argument lists in user-defined functions. This is implemented using the ... token in PHP 5.6 and later, and using the func_num_args(), func_get_arg(), and func_get_args() functions in PHP 5.5 and earlier.

http://php.net/manual/en/functions.arguments.php#functions.variable-arg-list

Here is for 5.6+

<?php

function my_func(...$parameters)
{
    echo "There were ".count($parameters)." parameters passed:\n";

    foreach ($parameters as $a_parameter)
    {
        echo "Number ".$a_parameter."\n";
    }
}

echo my_func(1, 2, 3, 4, 5);

?>

https://3v4l.org/QuJqD

In (even more) simple words:

The magic happens with the ... token. my_func expects a variable number of parameters, which will be stored in the $parameters array. All that because of the ... that precedes the $parameters. Then by using count on that $parameter array we get the number of the parameters stored in that array.

As Steven in the comments nicely put it: my_func(...$parameters) has a variable length parameter list as opposed to a fixed-length parameter list which would look like function my_func($param1, $param2, $param3, $param4, $param5) which would always expect 5 parameters.

Sign up to request clarification or add additional context in comments.

4 Comments

as opposed to a fixed-length parameter list which would look like function my_func($param1, $param2, $param3, $param4, $param5)
@StevenManuel nicely put Steven i ll add it to the answer
I changed static-length to fixed-length which I think is better
@StevenManuel "fixed" fixed.
1

To use variable length parameters on PHP you need the function func_get_args() instead to define the parameters on function declaration. Your function look like this:

function foo()
{
    $params_count = func_num_args();
    $params_values = func_get_args();
}

On $params_values there are all parameters which were given to the foo() function. On params_count there is the number of parameters given to foo(). You can get the number of parameters given to the foo function with func_num_args()

An example of using this functions (https://3v4l.org/TWd3v):

function foo() {
    $params_count = func_num_args();
    var_dump($params_count);
    $params_values = func_get_args();
    var_dump($params_values);
}

Comments

1

Function can take variable numbers of arguments You can use func_get_args to get all function arguments and works with them.

See http://php.net/manual/en/function.func-get-args.php

Comments

1
<?php
    function my_func(){
        $args = func_num_args();
        $args_list = func_get_args();
        echo 'There were '.$args.' numbers passed:<br>';
        foreach($args_list as $k=>$v){
            echo 'Number '.$v.'.<br>';
        }
    }

    my_func(1,2,3,4,5);
    my_func(3,4,5);
?>

Output 1:

There were 5 numbers passed:
Number 1.
Number 2.
Number 3.
Number 4.
Number 5.

Output 2:

There were 3 numbers passed:
Number 3.
Number 4.
Number 5.

Comments

1

PHP supports variable-length function, inorder to achieve that functionality you need to create a function with no params and you can get get that params using PHP function func_get_args()

function noprms(){
    $count = func_num_args();
    $args = func_get_args();
    echo "There were $count arguments passed \n";
    echo "Number ".implode(",\nNumber ",$args)."\n";
}

noprms(1,2,3,4,5)."\n";
noprms(1,2,3,4,5,6,7,8,9)."\n";

Docs Demo

Comments

0

I think the question refers to the triple dot notation: From the manual:

In PHP 5.6 and later, argument lists may include the ... token to denote that the function accepts a variable number of arguments.

The variable arguments are accessible as an array (so you can loop them).

Copy paste from the manual:

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.