Variadic Functions
Last updated
What is Variadic Functions?
Variadic functions in PHP are functions that can accept any number of arguments. This gives you greater flexibility, as it allows for an undetermined number of arguments. You can create a variadic function by adding '...' before the function argument. Any number of arguments you provide when calling the function are treated as an array, which can be processed using common array functions.
A simple code example:
function sum(...$numbers) {
return array_sum($numbers);
}
echo sum(1, 2, 3, 4);
This prints "10". The function accepts any number of arguments and adds them together.
Free resources to learn Variadic Functions
- Official docsVariadic Functions
More in stage 5: Functions
Variadic Functions: common questions
Can I learn Variadic Functions in Hindi?
This lesson is written in English. Once you sign in, open it in the interactive course and ask the AI tutor to explain Variadic Functions in Hindi or Hinglish. The tutor answers from this lesson’s own text, so it stays on the syllabus, and it runs on your own Google, OpenAI or Anthropic key.
Is this Variadic Functions tutorial for beginners?
It is lesson 38 of 120 in the free PHP roadmap, in stage 5, Functions. If you are new to PHP, start from stage 1 and come back here in order; if you already know the earlier stages, this lesson stands on its own.