14

Is there a way to define a function in PHP that lets you define a variable amount of parameters?

in the language I am more familiar with it is like so:

function myFunction(...rest){ /* rest == array of params */ return rest.length; }

myFunction("foo","bar"); // returns 2;

Thanks!

JD Isaacks
  • 51,154
  • 89
  • 267
  • 413

6 Answers6

37

Yes. Use func_num_args() and func_get_arg() to get the arguments:

<?php
  function dynamic_args() {
      echo "Number of arguments: " . func_num_args() . "<br />";
      for($i = 0 ; $i < func_num_args(); $i++) {
          echo "Argument $i = " . func_get_arg($i) . "<br />";
      }
  }

  dynamic_args("a", "b", "c", "d", "e");
?>

In PHP 5.6+ you can now use variadic functions:

<?php
  function dynamic_args(...$args) {
      echo "Number of arguments: " . count($args) . "<br />";
      foreach ($args as $arg) {
          echo $arg . "<br />";
      }
  }

  dynamic_args("a", "b", "c", "d", "e");
?>
John Conde
  • 207,509
  • 96
  • 428
  • 469
  • 3
    pretty sure `func_get_args ()` is what you actually want to use instead of this verbose first solution. – Toskan Jun 17 '14 at 05:14
6

You can accept a variable number of arguments to any function, so long as there are enough to populate all the declared arguments.

<?php

function test ($a, $b) { }

test(3); // error
test(4, 5); // ok
test(6,7,8,9) // ok

?>

To access the extra un-named arguments passed to test(), you use the functions func_get_args(), func_num_args(), and func_get_arg($i) :

<?php

// Requires at least one param, $arg1
function test($arg1) {

  // func_get_args() returns all arguments passed, in order.
  $args = func_get_args();

  // func_num_args() returns the number of arguments
  assert(count($args) == func_num_args());

  // func_get_arg($n) returns the n'th argument, and the arguments returned by
  // these functions always include those named explicitly, $arg1 in this case
  assert(func_get_arg(0) == $arg1);

  echo func_num_args(), "\n";
  echo implode(" & ", $args), "\n";

}

test(1,2,3); // echo "1 & 2 & 3"

?>
meager
  • 209,754
  • 38
  • 307
  • 315
3

Although this question is very old: Actually in PHP 5.6+ you can exactly write what you wrote :D

Franz
  • 49
  • 9
2

or just

function printArgs() {

 foreach (func_get_args () as $arg){
  echo $arg;
 }
}

printArgs("1 ", "2 ", "three ");

outputs 1 2 three

Toskan
  • 11,184
  • 12
  • 75
  • 144
1

I like to take a Javascript-esque approach with my PHP parameters. This allows for better setting of "options" and their defaults (which I'll look at momentarily). For example, let's say you have a function that returns a range of times in an array. Parameter 1 is the start time, parameter 2 is the end time, parameter 3 is the time interval, and any option after that is optional (like "format" => "24-hour", "include_seconds" => TRUE, etc.).

I would define the function like this:

function returnTimeInterval($startTime, $endTime, $interval, $options = array())
{
    // the first thing to do is merge options in with our defaults
    $options = array_merge(array(
        "format" => "24-hour",
        "include_seconds => TRUE
        // etc.
    ), $options);

This allows for the setting of defaults within the function that can be then overridden, which is pretty cool. Of course, you need to take care that strange, un-used options aren't passed in, but I'll leave that to you. :)

ABach
  • 3,592
  • 4
  • 22
  • 31
0

I would encourage you to pass an array into your function, that way you can have as many different parameters stored in that array. There are many operations you can make on the array once it is in the function to get the right info you need.

$array = array();
$array[0] = "a";
$array[1] = 1;

myFunction($array);
Sam152
  • 17,679
  • 13
  • 55
  • 76