How do default parameter values and variadic functions (using ...) work in PHP?

Last updated 15 June 2026

Default parameter values let a caller omit trailing arguments by giving them a fallback in the signature, while variadic functions use ... to collect any number of extra arguments into an array (or to spread an array into arguments).

  • Default parameter values:

    • Assigned in the signature: function f($x, $y = 10); omitting $y uses 10.

    • Defaults must be constant expressions and should come after required parameters.

  • Variadic parameter (...$args):

    • Captures remaining arguments as an array; must be the last parameter.

    • Can be type-hinted: function sum(int ...$nums) requires all extras to be ints.

  • Spread operator: The same ... at the call site unpacks an array into individual arguments: sum(...[1, 2, 3]).

[lang:php] function greet($name, $greeting = 'Hello') { return "$greeting, $name"; } greet('Ann'); // "Hello, Ann" function sum(int ...$nums) { return array_sum($nums); } sum(1, 2, 3); // 6 sum(...[4, 5, 6]); // 15 (spread)

  • https://www.php.net/manual/en/functions.arguments.php
  • https://www.php.net/manual/en/migration56.new-features.php
  • https://dev.to/sharminshanta/variadic-function-in-php-49c1
  • https://dev.to/sharminshanta
  • https://qubitsandbytes.co.uk/php/using-the-php-splat-operator/
  • https://dev.to/iamjnayem/default-parameters-in-php-functions-35en
  • https://medium.com/@annxsa/common-php-interview-questions-with-clear-tested-answers-fb94ec4306bc
  • https://markbakeruk.net/2021/09/28/named-arguments-and-variadics-in-php-8/
All PHP interview questions