The PHP variadic are the functions that accept more than one argument according to the passed spread operator ... in the parameter.

The basic expression of the variadic function would be like the below.

function name( ...$param ) {
  // .... Your code here
}

An Overview for PHP Variadic Functions

The PHP variadic functions are accepting the three dots operator to define unlimited arguments when you invoke this function in another place during the stack.

The variadic function is similar to the PHP built-in function: func_get_args() which gets all passed arguments in the function.

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 the below.

Array ( [0] => 55 [1] => 16 )

That means the func_get_args() is getting all arguments that passed into the function when invoking it in another place.

PHP Variadic Functions

The PHP variadic functions have the same job which accepts the three dots operator beside the variable. So to define a new variadic function you have to pass these three dots beside its argument. For example.

<?php
   function mul( ...$numbers ) { 
     print_r($numbers);
   }
   mul(55, 16);
?>

It will print the same result of the above example.

Anyway, Let’s take more examples.

Examples of PHP Variadic Functions

Concatenate the passed arguments together using the variadic function.

<?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 who you an error as the below.

Fatal error: Uncaught TypeError: func(): Argument #1 must be of type object

To fix that, you have to pass 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 position of the variadic would be at the last parameter in the function, Otherwise it will show you an error. For example.

<?php
  function table( ...$x, $y ) {
      // ...
  }

  table(10, 10);  
?>

This will show you an error like the below.

Fatal error: Only the last parameter can be variadic in index.php

So the correct syntax would be as the below.

<?php
  function table( $y, ...$x ) {
      // ...
  }

  table(10, 10);  
?>

Wrapping Up

  • The variadic referring to unlimited arguments when you pass the three dots into the function parameter.
  • The variadic position would be placed as the last parameter of the function to avoid the fatal error.
  • The variadic function is a new feature for PHP 5.6.
  • You can unpack an array instead of passing a function argument in the variadic function. callback( $var, ...['va', 'ca'] ).
  • The variadic parameter will show you as an array within the function.
  • The variadic function can be defined by adding the three dots beside the argument variable ... .