Variadic functions in PHP are functions that can accept a variable number of arguments using the spread operator “…” in the function parameter.
The basic syntax of a variadic function in PHP would look like the following:
function name( ...$param ) {
// .... Your code here
}
An Overview of PHP Variadic Functions
PHP variadic functions are defined using the three dots operator before the function parameter, which allows the function to accept an unlimited number of arguments when called in other parts of the code.
Variadic functions in PHP are similar to the built-in function func_get_args()
, which retrieves all the arguments passed to a function. The difference is that with variadic functions, you can define the arguments in the function signature itself, making the code more concise and easier to read.
For example.
<?php
function mul( $number1, $number2 ) {
$args = func_get_args();
print_r($args);
}
mul(55, 16);
?>
In this example, the output would be an array as shown below.
Array ( [0] => 55 [1] => 16 )
This means that func_get_args()
retrieves all the arguments passed into the function when it is invoked in other parts of the code.

PHP variadic functions perform the same function by allowing the use of the three dots operator before the function parameter to accept a variable number of arguments. To define a new variadic function, you simply need to add the three dots before the function parameter, as in the following example:
<?php
function mul( ...$numbers ) {
print_r($numbers);
}
mul(55, 16);
?>
It will print the same result as the previous example. Now, let’s look at some more examples.
Examples of PHP Variadic Functions
This variadic function concatenates all the passed arguments together.
<?php
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", "CodedTag", "Tutorials" );
echo $welcome;
?>
The output:
Welcome to CodedTag Tutorials
Using the variadic function with a type hinting.
<?php
function func( Object ...$obj ) {
print_r( $obj );
}
func([1, 2, 3]);
?>
This will show you an error as shown below.
Fatal error: Uncaught TypeError: func(): Argument #1 must be of type object
To fix this error, you need to pass arguments of the same data type.
<?php
$obj = (object) [1, 2, 3];
func($obj);
?>
The output:
Array ( [0] => stdClass Object ( [0] => 1 [1] => 2 [2] => 3 ) )
Unpack an array into arguments using the variadic function.
<?php
function table( $w, $x, $y, $z ) {
echo ( ($w + 20 ) - ( $y + $z ) );
}
table(10, ...[20, 30, 40]); // -40
?>
The variadic parameter must be the last parameter in the function, otherwise, an error will occur. For example:
<?php
function table( ...$x, $y ) {
// ...
}
table(10, 10);
?>
This will show you an error as shown below.
Fatal error: Only the last parameter can be variadic in index.php
The correct syntax would be as follows:
<?php
function table( $y, ...$x ) {
// ...
}
table(10, 10);
?>
Wrapping Up
Variadic functions in PHP allow you to accept a variable number of arguments by using the three dots operator before the function parameter. This means that you can pass an unlimited number of arguments into the function when you call it in other parts of your code.
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.
Variadic functions were introduced in PHP 5.6 as a new feature that allows developers to write more flexible and powerful code. By using variadic functions, you can make your code more concise and easier to read.
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.