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)

Jetbrains image

Build Secure, Ship Fast

Discover best practices to secure CI/CD without slowing down your pipeline.

Read more

👋 Kindness is contagious

Dive into this thoughtful article, cherished within the supportive DEV Community. Coders of every background are encouraged to share and grow our collective expertise.

A genuine "thank you" can brighten someone’s day—drop your appreciation in the comments below!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found value here? A quick thank you to the author makes a big difference.

Okay