Variadic functions

One function, any number of arguments: PHP does it with three dots on both sides of the call. On the parameter list they collect the extras, on a call they unpack an array into separate values. The examples count and join the packed array with array_sum() and implode().

Collect ...

Three dots before the last parameter collect the rest into one array. The function then accepts zero or more values in that slot.

Name the packed array in plural so calls read well and loops stay clear.

<?php function sumAll(int ...$nums): int { return array_sum($nums); } echo sumAll(1, 2, 3), PHP_EOL; echo sumAll(), PHP_EOL;

php splat.php

6
0

The call with no arguments is legal because the packed slot may be empty, and the sum of an empty list is zero.

Spread calls

Three dots also unpack an array into separate arguments at call time. One packed list can feed any function that expects single values.

Use spread to forward arrays into helpers without writing long argument lists by hand.

<?php function sumAll(int ...$nums): int { return array_sum($nums); } $list = [4, 5, 6]; echo sumAll(...$list), PHP_EOL; echo sumAll(...[1, 2], ... [3]), PHP_EOL;

php spread.php

15
6

The array arrives as three separate integers, and two spreads on one call are joined in the same way.

Mixed fixed+variadic

Fixed parameters come first and the packed part comes last. Callers always pass the fixed values, then add as many extras as needed.

This shape suits loggers where level stays required while message parts vary.

<?php function logMsg(string $level, string ...$parts): string { return $level . ": " . implode(" ", $parts); } echo logMsg("info", "cache", "warm"), PHP_EOL; var_dump(logMsg("warn"));

php mixed.php

info: cache warm
string(6) "warn: "

The level is always there, the two parts are joined with a space, and a call with no parts still returns the level with its colon.

func_get_args legacy note

Older code reads extra arguments with func_get_args and friends. It works but hides the signature from readers and tools.

Prefer real variadics in new code and keep the legacy helpers only when patching old functions.

<?php function legacySum(): int { return array_sum(func_get_args()); } echo legacySum(1, 2, 3), PHP_EOL; echo legacySum(1, 2), PHP_EOL;

php funcargs.php

6
3

The signature says the function takes nothing, yet both calls work: the arguments are read at run time, which is exactly the hidden behaviour the prose warns about.

Exercises

  1. Write a variadic function that returns the largest of its arguments and print the result of an empty call.
  2. Spread an array of names into a function that takes two fixed strings and one variadic part.
  3. Print the number of arguments a variadic function received.
  4. Call sprintf() with a spread array that holds the format and its values.
  5. Rewrite a func_get_args() function as a variadic one and compare the two signatures.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does the ... before a parameter do?

    Answer

    It collects the remaining arguments into an array

    The variadic parameter receives every extra argument as one array.

  2. What does ... before an array at a call site do?

    Answer

    It unpacks the array into separate arguments

    Spread turns each element into an argument of the call.

  3. Where does the variadic parameter have to sit in the signature?

    Answer

    Last, after the fixed parameters

    It takes everything that is left, so no fixed parameter can follow it.

  4. Why is func_get_args() discouraged in new code?

    Answer

    It hides the real signature from a reader and an editor

    The signature shows nothing, so neither a reader nor an editor can see what the function takes.

  5. What does this program print? <?php function sumAll(int ...$nums): int { return array_sum($nums); } echo sumAll(1, 2, 3);

    Answer

    6

    The three integers land in the array and array_sum() adds them up.

  6. What does this program print? <?php function sumAll(int ...$nums): int { return array_sum($nums); } var_dump(sumAll());

    Answer

    int(0)

    The packed array may be empty, and the sum of nothing is the integer zero.

  7. What does this program print? <?php function sumAll(int ...$nums): int { return array_sum($nums); } $list = [4, 5, 6]; echo sumAll(...$list);

    Answer

    15

    The spread passes 4, 5 and 6 as separate integers, so the sum is 15.

  8. What does this program print? <?php function logMsg(string $level, string ...$parts): string { return $level . ": " . implode(" ", $parts); } echo logMsg("info", "cache", "warm");

    Answer

    info: cache warm

    The level comes first and the two parts are joined with a single space.

  9. What does this program print? <?php function legacySum(): int { return array_sum(func_get_args()); } echo legacySum(1, 2, 3);

    Answer

    6

    PHP does not enforce an empty signature for extra arguments, and func_get_args() reads all three.

  10. What does this program print? <?php function firstTwo(string $a, string $b, string ...$rest): string { return $a . $b . count($rest); } echo firstTwo("a", "b", "c", "d");

    Answer

    ab2

    The two fixed parameters take a and b, and the rest array holds c and d, so the count is two.

The code on this page was run with PHP 8.5.13 (php -v) in Alpine Linux, with the commands shown above.

Article author: Arthur Isaev