Use Named Arguments with PHP Variadic Parameters

A PHP variadic parameter can collect unknown named arguments, but their names are preserved as string keys. Use named arguments when the keys carry meaning; use positional arguments when the function expects a simple ordered list.

Last updated: October 26, 2026.

<?php
function configure(string $service, string ...$options): array
{
    return ['service' => $service, 'options' => $options];
}

$named = configure(
    service: 'mailer',
    transport: 'smtp',
    encryption: 'tls'
);
// options: ['transport' => 'smtp', 'encryption' => 'tls']

$positional = configure('mailer', 'smtp', 'tls');
// options: [0 => 'smtp', 1 => 'tls']

Both calls satisfy the string ...$options declaration. The difference is the shape of the captured array: unknown named arguments keep their names, while positional arguments receive numeric keys.

Do not repeat the variadic parameter name

The PHP function-arguments documentation explains both variable-length argument lists and named arguments. A call such as configure(options: "smtp") creates one entry whose key is options. The parameter name is not a container that can be specified repeatedly, so passing options: twice causes a duplicate named-parameter error.

PHP also forbids an ordinary positional argument after a named argument. If you need an ordered list, put every positional value first. If you need a keyed option map, give each extra argument its own meaningful name as in the example.

Unpack arrays with the intended key type

The named-arguments specification states that string-keyed values supplied through argument unpacking are treated as named arguments, while integer-keyed values are positional. Therefore configure(...["service" => "mailer", "transport" => "smtp"]) preserves the option name. A list such as ["mailer", "smtp"] remains positional.

Parameter names become part of the callable interface when callers use them, so renaming a public parameter can break consumers. Prefer a single explicit array parameter for a large or evolving configuration schema, especially when validation, defaults, or nested options are required. Continue with PHP function parameters and return types, explicit nullable parameters, and safe array traversal.

Related Web Cheat Sheet guides

Sergey Kornilov

Sergey Kornilov