func_get_args
will do what you want:
function infinite_parameters() {
foreach (func_get_args() as $param) {
echo "Param is $param" . PHP_EOL;
}
}
You can also use func_get_arg
to get a specific parameter (it's zero-indexed):
function infinite_parameters() {
echo func_get_arg(2);
}
But be careful to check that you have that parameter:
function infinite_parameters() {
if (func_num_args() < 3) {
throw new BadFunctionCallException("Not enough parameters!");
}
}
You can even mix together func_*_arg
and regular parameters:
function foo($param1, $param2) {
echo $param1; // Works as normal
echo func_get_arg(0); // Gets $param1
if (func_num_args() >= 3) {
echo func_get_arg(2);
}
}
But before using it, think about whether you really want to have indefinite parameters. Would an array not suffice?