Get "PHP 8 in a Nuthshell" (Now with PHP 8.4)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

Variable-length function arguments using spread token in PHP

When working with functions, there might come the scenario where you’re unsure as to how many arguments a function would have. Basically, a function can have an unlimited number of arguments that you don’t define in the function signature.

For instance, a function that takes multiple strings as its arguments and counts the sum of words from all the specified strings. But there’s no limit on how many strings you could provide to the function. So, the calling function could look like so.

wordCount('Hello world', 'What is up?');

wordCount('Amit Merchant', 'Software Developer', 'Male');

wordCount('red', 'green', 'blue', 'pink');

As you can tell, the wordCount function takes in a different number of arguments when calling it. So, how do you define such a function which can handle variable-length arguments.

The spread (...) token

Essentially, you can use the ... token to assign variable-length argument lists to user-defined functions. The arguments will be passed into the given variable as an array.

So, if we want to write the wordCount function using the ... token, we can do it like so.

function wordCount(...$strings) {
    $totalWords = 0;

    foreach ($strings as $string) {
        $totalWords += str_word_count($string);
    }

    return $totalWords;
}

As you can tell, the variable $string is an array that contains all the strings provided to the function wordCount when calling it.

Conversely, you can also make the number of arguments fixed and provide the arguments using ... like so.

function wordCount($string1, $string2) {
    $totalWords = 0;

    $totalWords = str_word_count($string1) + str_word_count($string2);

    return $totalWords;
}

$count = wordCount(...['Amit', 'Developer']);
Learn the fundamentals of PHP 8 (including 8.1, 8.2, 8.3, and 8.4), the latest version of PHP, and how to use it today with my new book PHP 8 in a Nutshell. It's a no-fluff and easy-to-digest guide to the latest features and nitty-gritty details of PHP 8. So, if you're looking for a quick and easy way to PHP 8, this is the book for you.

Like this article?

Buy me a coffee

👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.

Comments?