Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

newbie360's avatar

About PHP ...$params

While coding, i just wonder why this work ?

class FetchService
{
    public function getById($id)
    {
        if (true) {
            // Read be careful, i'm not passing array
            $this->exitBrowser("[%s] 🔔 id is invalid format (%s)", 
                now()->toTimeString(),
                $id,
            );
        }
    }

    public function exitBrowser(...$params)
    {
        $this->dddd(...$params);
        
        $this->browser->quit();
        $this->browser->stop();
    }

    public function dddd(...$params)
    {
        dump(sprintf(...$params));
    }
}
$fetch = new FetchService();
$fetch->getById('something wrong');

//result
[21:00:00] 🔔 id is invalid format (something wrong)

Why keep passing ...$params and this sprintf(...$params) work?

0 likes
11 replies
tykus's avatar

The ... operator (called rest or splat in other languages) can be used for variadic functions, i.e. where the function can accept variable numbers of arguments.

It is also usable as a spread operator to unpack arrays or traversables into function parameters, or spread into other arrays, e.g.

$parts = ['apple', 'pear'];
$fruits = ['banana', 'orange', ...$parts, 'watermelon'];

https://www.php.net/manual/en/functions.arguments.php#functions.variable-arg-list

1 like
newbie360's avatar

@tykus But $params is array and sprintf() first param accept string only

tykus's avatar
tykus
Best Answer
Level 104

@newbie360 yes, I explained that the spread operation unpacks arrays or traversables into function parameters. The exitBrowser function is passed three parameters:

  1. "[%s] 🔔 id is invalid format (%s)"
  2. now()->toTimeString()
  3. $id

In the function's signature, they are packed into the $params array. However, they are then unpacked again (using ...) whenever you call sprintf

1 like
newbie360's avatar

@tykus If i add a line before sprintf(), it is array

    public function dddd(...$params)
    {
        dump(...$params); // this return array

        dump(sprintf(...$params));
    }

so WHEN it auto unpack ?

tykus's avatar

@newbie360 it unpacks here sprintf(...$params) so equivalent of:

sprintf("[%s] 🔔 id is invalid format (%s)",  now()->toTimeString(), $id)
newbie360's avatar

@tykus OK, so dddd(...$params) is receive '....., ....., ......' or array ?

tykus's avatar

@newbie360 same as above... you unpack an array $params into dddd, equivalent of:

dddd("[%s] 🔔 id is invalid format (%s)",  now()->toTimeString(), $id)

TBH I don't see the need for dddd in the context above; it is just a wrapper around dump(sprintf()) - it adds nothing...

newbie360's avatar

@tykus I use this very long time ago

    public function something($params)
    {
        $params = is_array($params) ? $params : func_get_args();

    }

could i use ...$params instead ?

Edit: the dddd() just a wrapper, but use anywhere to dump in console, the exitBrowser() is for close the browser

tykus's avatar

@newbie360 essentially, yes. Just be aware that $params will always be an array whenever the function is variadic

newbie360's avatar

may be i give the best answer to the first reply.

@tykus OK thanks

@amitsolanki24_ your reply very detail, actually i know what is ...$params, but i just wonder why it can auto unpack, good day

amitsolanki24_'s avatar

#AI deep description

I hope you can easily understand.

1 like

Please or to participate in this conversation.