DEV Community

Sharmin Shanta
Sharmin Shanta

Posted on

1

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)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay