Variadic Function in PHP
Sharmin Shanta

Sharmin Shanta @sharminshanta

About: Backend Engineer | 5+ yrs | PHP | Python | Microservices | MySQL | MongoDB | Redis | Elasticsearch | JavaScript

Location:
Dhaka, Bangladesh
Joined:
Oct 3, 2024

Variadic Function in PHP

Publish Date: Dec 4 '24
1 0

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

Comments 0 total

    Add comment