ZetCode

PHP variadic functions

last modified October 22, 2026

A variadic function is a function that accepts a variable number of arguments. PHP supports this in two ways: the variadic parameter declared with ...$args, and the older func_get_args family of functions that read all passed arguments at runtime.

Variadic parameter

A parameter prefixed with ... collects all remaining arguments into an array. It must be the last parameter in the function signature.

basic_variadic.php
<?php

function sum(...$numbers) {

    $total = 0;

    foreach ($numbers as $n) {
        $total += $n;
    }

    return $total;
}

echo sum(1, 2, 3), "\n";
echo sum(10, 20), "\n";
echo sum(), "\n";

The sum function accepts any number of arguments. Inside the function, $numbers is an ordinary array. When no arguments are passed, the array is empty.

$ php basic_variadic.php
6
30
0

Required parameters before variadic ones

Regular parameters can appear before the variadic parameter. They are filled first, and the rest of the arguments go into the array.

greet.php
<?php

function greet(string $greeting, string ...$names) {

    foreach ($names as $name) {
        echo "$greeting, $name!\n";
    }
}

greet("Hello", "Jane", "Rose", "Patrick");

The first argument is bound to $greeting; the remaining arguments are collected into $names.

$ php greet.php
Hello, Jane!
Hello, Rose!
Hello, Patrick!

A variadic parameter cannot be followed by another parameter, and it cannot have a default value. Calling with too few arguments for the required parameters raises an ArgumentCountError.

Type declarations

A type declaration on a variadic parameter applies to each collected argument, not to the array itself.

typed_variadic.php
<?php

declare(strict_types=1);

function average(int ...$values): float {

    if (count($values) === 0) {
        return 0.0;
    }

    return array_sum($values) / count($values);
}

echo average(2, 4, 6), "\n";
echo average(), "\n";

try {
    echo average(1, "two", 3), "\n";
} catch (TypeError $e) {
    echo "TypeError: ", $e->getMessage(), "\n";
}

Each argument must be an int. With strict_types enabled, passing a string raises a TypeError.

$ php typed_variadic.php
4
0
TypeError: average(): Argument #2 must be of type int, string given

Passing arrays to variadic functions

To pass the elements of an array as individual arguments, use the splat operator on the call site. This is covered in more detail in the splat operator article.

unpack_call.php
<?php

function sum(...$numbers) {
    return array_sum($numbers);
}

$values = [4, 8, 15, 16, 23, 42];

echo sum(...$values), "\n";

The ...$values on the call site unpacks the array into separate arguments, which are then collected by the variadic parameter in sum.

$ php unpack_call.php
108

func_get_args

Functions can also read their arguments without declaring a variadic parameter. func_get_args returns an array of all arguments passed to the current function. func_num_args returns how many arguments were passed, and func_get_arg returns a single argument by its zero-based position.

func_get_args.php
<?php

function maximum() {

    $args = func_get_args();

    if (count($args) === 0) {
        return null;
    }

    $max = $args[0];

    foreach ($args as $arg) {
        if ($arg > $max) {
            $max = $arg;
        }
    }

    return $max;
}

function report() {
    echo "received ", func_num_args(), " arguments; second is ";
    echo func_get_arg(1), "\n";
}

echo maximum(3, 9, 2), "\n";
report(1, 2, 3, 4);

The maximum function has no declared parameters, yet it works with any number of arguments. It reads them all with func_get_args and returns the largest. The report function uses func_num_args to count the arguments and func_get_arg(1) to read the second one, which is zero-based. func_get_args is only available inside a function body.

$ php func_get_args.php
9
received 4 arguments; second is 2

Why prefer ...$args

func_get_args hides the function's interface: IDEs, static analysis tools, and readers cannot see the expected arguments, and no type checks are applied. The variadic parameter is declared in the signature, supports type declarations, and is clearer to read. Use func_get_args mainly when maintaining older code or when a function must accept arguments that cannot be declared.

Source

PHP variable-length argument lists

func_get_args manual

In this article we have covered variadic functions in PHP.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all PHP tutorials.