Table of Content
PHP Variadic functions show you a way to handle a variable number of arguments within a function. They are designed to accept a variable number of arguments through the use of the spread operator “…” in the function parameter.
Let’s take a look at the syntax.
Syntax of Variadic Functions
You need to place the ellipsis (…) before the last parameter in the function signature when you declare a variadic function. For example:
function name( ...$param ) {
// .... Your code here
}Let’s move to the following section to explain how it works inside functions.
How the Variadic Functions Work in PHP
In PHP, you use three dots (…) before the parameter to define a variadic function. This enables the function to accept an unlimited number of arguments when invoked elsewhere in the code.
Variadic functions work like the built-in func_get_args() function, which gets all the arguments sent to a function.
But the main difference is that variadic functions let you list the arguments in the function’s header. This makes your code shorter and easier to read.
For example.
function mul( $number1, $number2 ) {
$args = func_get_args();
print_r($args);
}
mul(55, 16);Here is the output:
Array ( [0] => 55 [1] => 16 )
This implies that func_get_args() captures all the arguments passed into the function when invoked in other sections of the code.
PHP variadic functions do the same job. You put three dots (…) before the parameter to accept any number of arguments.
Here is an example:
function mul( ...$numbers ) {
print_r($numbers);
}
mul(55, 16);It will print the same result as the previous example.
Examples of PHP Variadic Functions
This variadic function concatenates all the provided arguments.
function variadic_func( ...$param ) {
$string = '';
if( count( $param ) ) {
for( $i=0; $i<count($param); $i++ ) {
$string .= $param[$i];
$string .= " ";
}
}
return $string;
}
$welcome = variadic_func( "Welcome", "to", "FlatCoding", "Tutorials" );
echo $welcome;The output:
Welcome to FlatCoding Tutorials
Use the variadic function with a type hint.
function func( Object ...$obj ) {
print_r( $obj );
}
func([1, 2, 3]);This produces an error. Here is the output:
Fatal error: Uncaught TypeError: func(): Argument #1 must be of type object.
To address this error, ensure that you pass arguments of the same data type.
$obj = (object) [1, 2, 3];
func($obj);The output:
Array ( [0] => stdClass Object ( [0] => 1 [1] => 2 [2] => 3 ) )
Unpack an array into arguments with the variadic function.
function table( $w, $x, $y, $z ) {
echo ( ($w + 20 ) - ( $y + $z ) );
}
table(10, ...[20, 30, 40]); // -40The variadic parameter must be the last parameter in the function; otherwise, an error will occur.
For example:
function table( ...$x, $y ) {
// ...
}
table(10, 10);The output:
Fatal error: Only the last parameter can be variadic in index.php
The correct syntax would be as follows:
function table( $y, ...$x ) {
// ...
}
table(10, 10);Wrapping Up
Variadic functions in PHP let you take any number of arguments. You put three dots (…) before the parameter. This way, you can pass as many arguments as you want when you call the function.
To prevent fatal errors, the variadic parameter must always be placed at the end of the function parameter list. This ensures that the function can correctly parse the arguments and avoid any errors that may occur.
PHP introduced variadic functions in version 5.6.
In addition to passing individual arguments, you can also unpack an array into a variadic function. For example, you can call a function and pass an array of arguments like this: callback($var, ...['va', 'ca']). This will unpack the array and pass its values as separate arguments to the function.
When you define a variadic function in PHP, the variadic parameter is treated as an array within the function. This means that you can loop through the arguments and perform operations on them as needed.
What is a variadic function in PHP?
How do you declare a variadic function in PHP?
function example( ...$params ) {
// code here
}
What is the difference between variadic functions and func_get_args()?
Where must the variadic parameter be placed in the function signature?
How do you pass an array of arguments to a variadic function?
table(10, ...[20, 30, 40]);
Similar Reads
The array_intersect_key in PHP compares arrays by keys and returns only the parts that match. It is useful when you…
PHP 5 added support for classes and interfaces. This made object-oriented code possible with one feature being polymorphism. That means…
Understanding PHP syntax is like the basics of any language—PHP syntax defines the rules for writing code that a server…
Whether you are just trying to keep your app from crashing or making sure your users’ uploads don’t accidentally overwrite…
Sometimes the string you expect is there, but strpos function gives a surprise in PHP. It may show the result…
If you've ever tried to pull data from an XML file, you know it can be a bit tricky at…
The PHP ternary operator gives you a way to write short expressions. It reduces lines of code and avoids long…
File Handling in PHP is an important tool especially when you need to manage files such as reading, writing, or…
The PHP “while” loop is a simple tool that repeatedly runs a block of code as long as a certain…
PHP developers copied and pasted code across multiple files before inheritance came, which made updates difficult. They updated functions in…