PHP Variadic Functions
Variadic functions accept a variable number of arguments using the splat operator (...). Perfect for flexible functions like sum(), max(), or custom utilities!
Output
Click Run to execute your code
Variadic Parameters
<?php
function sum(...$numbers) {
return array_sum($numbers);
}
echo sum(1, 2, 3); // 6
echo sum(1, 2, 3, 4, 5); // 15
?>
Mixed Parameters
<?php
function formatList($separator, ...$items) {
return implode($separator, $items);
}
echo formatList(", ", "apple", "banana", "cherry");
?>
Summary
- Syntax:
...$params - Variable args: Accept any number
- Type hints:
int ...$nums - Unpacking:
func(...$array)
What's Next?
Finally, learn about Function Scope - global, local, and static variables!
Enjoying these tutorials?