DEV Community

Sharmin Shanta
Sharmin Shanta

Posted on

Variadic Function in PHP

A variadic function is a function that accepts a variable number of arguments with three dots(...) called the spread operator before the function parameter. A function can have many parameters, but sometimes It isn't easy to maintain. For this reason, PHP has the facility to use the variadic function. This type of parameter should be the last variable parameter in the function signature. It returns in the function as an array.

<?PHP

/**
 * @param $var1
 * @param $var2
 * @param ...$array
 * @return array
 */
function variadicFun($var1, $var2, ...$array)
{
    return $array;
}

print_r(variadicFun('name', 'age', 'Devid', 'Gilmour', 'Shwan'));
Enter fullscreen mode Exit fullscreen mode

Top comments (0)