PHP Variadic Functions

Summary: in this tutorial, you’ll learn about the PHP variadic functions that accept a variable number of arguments.

Introduction to the PHP variadic functions

So far, you’ve learned how to define functions that accept a fixed number of parameters. A variadic function accepts a variable number of parameters.

The following example defines a function called sum() that returns the sum of two integers:

<?php function sum(int $x, int $y) { return $x + $y; } echo sum(10, 20); // 30
Code language: PHP (php)

To allow the sum() function to accept a variable of arguments, you need to use func_get_args() function. The func_num_args() function returns an array that contains all function arguments. For example:

<?php function sum() { $numbers = func_get_args(); $total = 0; for ($i = 0; $i < count($numbers); $i++) { $total += $numbers[$i]; } return $total; } echo sum(10, 20) . '<br>'; // 30 echo sum(10, 20, 30) . '<br>'; // 60
Code language: PHP (php)

In this example, we don’t specify any parameter for the sum() function. When calling the sum() function, we can pass any number of arguments into it.

Inside the function, the func_get_args() returns an array that contains all the arguments. To sum up all the arguments, we use a for loop.

PHP 5.6 introduced the ... operator. When you place the ... operator in front of a function parameter, the function will accept a variable number of arguments, and the parameter will become an array inside the function. For example:

<?php function sum(...$numbers) { $total = 0; for ($i = 0; $i < count($numbers); $i++) { $total += $numbers[$i]; } return $total; } echo sum(10, 20) . '<br>'; // 30 echo sum(10, 20, 30) . '<br>'; // 60
Code language: PHP (php)

In this example, we place the ... operator in front of the $numbers argument. The sum() function now accepts a variable of number arguments.

PHP 7 allows you to declare types for variadic arguments. For example:

<?php function sum(int ...$numbers): int { $total = 0; for ($i = 0; $i < count($numbers); $i++) { $total += $numbers[$i]; } return $total; }
Code language: PHP (php)

In this example, the sum() function will accept only integers and return the sum of integers.

If a function has multiple parameters, only the last parameter can be variadic. For example:

function my_func($a, $b, ...$params) { // ... }
Code language: PHP (php)

If you place the variadic parameter before a regular one, you’ll get an error:

Fatal error: Only the last parameter can be variadic in...
Code language: PHP (php)

And a function only has one variadic parameter.

Note that you can use the array_sum() to calculate the sum of all elements of an array. So you can simplify the sum() function like this:

<?php function sum(int ...$numbers): int { return array_sum($numbers); }
Code language: PHP (php)

Summary

  • A variadic function accepts a variable number of arguments.
  • Do use the ... operator to define a variadic function.
  • Only the last parameter can be variadic.
Did you find this tutorial useful?